Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] @PrePersist failing on InheritanceType.TABLE_PER_CLASS

Thanks Guy.  That way it, I wasn't aware that was the case.  I appreciate the help.

On Mon, Feb 13, 2012 at 3:30 PM, Guy Pelletier <guy.pelletier@xxxxxxxxxx> wrote:
Hi Mike,

I believe this is currently working as expect. Since the EntityListener is defined on the ManagedEntity mapped superclass, it will only be inherited by the immediate subclasses (ChildClass and ParentClass).

To have the same listener apply to InheritedChildA you will need to add an @EntityListener(ManagedEntityListener.class) to it.

Alternatively you may also add a default listener in orm.xml under persistence-unit-defaults which is applied to all classes of the persistence unit (if that suits your model).

Cheers,
Guy

On 13/02/2012 4:20 PM, Mike Key wrote:
Typo on ParentClass, it should also extend ManagedEntity.

On Mon, Feb 13, 2012 at 1:53 PM, Mike Key <mikey@xxxxxxxxxxx> wrote:
At least I believe that is what is happening.  I am using EclipseLink 2.3.2 where I have an entity that has a one-to-many with a concrete child entity and an inheritance structure on another child entity using TABLE_PER_CLASS inheritance.  I have a class that marks all entities with a createDate using JPA @PrePersist.  On the concrete entity this works fine, however it does not update the entity being created that is using inheritance.  I get a MySQLIntegrityConstraintViolation as the create_date column is required but not being created on the inherited class prior to persist.  I've seen a few rumblings similar to this elsewhere but nothing directly regarding @PrePersist.  Any help greatly appreciated.

Things look like this:

public class ManagedEntityListener {

    @PrePersist
    public void prePersist(Object entity) {
            final CSPContext context = CSPContextHolder.getContext();
            ((ManagedPlatformEntity) entity).setCreateTime(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime());
        }
    }
}

// All classes inherit from this mapped superclass for createDate property
@MappedSuperclass
public abstract class ManagedEntity() {
    @Column(name = "csp_create_dtt", nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
     protected Date createTime;
...
}

// Parent class
@Entity
public class ParentClass() {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    ....
// Concrete Child
   @OneToMany(cascade = CascadeType.PERSIST,
               fetch = FetchType.LAZY,
               targetEntity = ChildClass.class)
    @JoinColumn(name = "parent_class_id")
    private List<ChildClass> addressList = new ArrayList<ChildClass>();

// Inheritance Child

 @OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY,  orphanRemoval = true)
    @JoinColumn(name = "parent_class_id")
    private List<InheritanceChild> inheritanceChild;

...
}


// Concrete ChildClass
@Entity
@Table(name = "child_class")
public class ChildClass() extends ManagedEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "fieldA")
    private String fieldA;
    @Column(name = "fieldB")
    private String fieldB;
    @Column(name = "fieldC")
    private String fieldC;
    @Column(name = "csp_create_dtt", nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)

...
}

// InheritanceChild base class

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class InheritanceChild extends ManagedEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private Account account;

   // getters and setters for Account and Id
...
}

// ConcreteInheritedChild
@Entity()
@Table(name = "inherited_child_a")
public class InheritedChildA extends InheritanceChild {

    @Column(name = "some_field")
    private String someField;
}


Again in this structure, ChildClass is populated with createDate but InheritedChildA does NOT populate the createDate and throws a constraint violation from MySQL.  Thanks in advance for any insight on this.

Mike Key

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top