`

JPA Notes 2

    博客分类:
  • JPA
阅读更多

When entity got detached

  • when transaction is over, commited
  • when application-managed persistence context is closed
  • stateful bean is removed, its persistence context will be removed
  • clear method call from EntityManager
  • transaction rollback
  • when entity is serialized

Merge process

  • merge will update the managed entity if there is one.
  • merge will include new entity COPY into context
  • manager will check whether there is one in Database, if there is, load it, and ignore the detached one.
  • cascading operation is disabled as default , same as merge, if merge is not set in the relationship, no merge operation as cascaded.

Merge lazy-loading entity

  • if lazy-loading relationship was not triggered before detached, the relationship will be ignored.
  • if its triggered while managed and set to null while detached, then managed version will have relationship cleared during merge

JPA QL

  1. dynamic query
  2. named query

set query parameter

you can use object to pass to a query, but when set date time, we use the following format

quuery.setParameter(1, startDate, TemportalType.Date)

 

When pass object to as parameter, the object ID will be used in where clause

 

@Embeded and @Embedable

@attributeOverrides can has element @AttributeOverride to orverride attribute properties.

 

@compound primary key - I dont like it personally

  • Primary key classes - include definition: equals() hashcode(), serializable, and with no-arg constructor, annotated with @IdClass
  • embeddedId, use @Embedable to define primary key class

 

Inheritance strategy

 

Single Table

  1. default, use @Inheritance(Strategy=InteritanceType.Single_Table) for Parent class.
  2. use @discriminatorColumn to differenciate sub-classes.
  3. Only concrete class can have @discriminatorValue specified.
  4. If no specified, default value will be used, the entity name
  5. @Enity(name="PTemp"), in this case, "PTemp" will be used as discriminator value

Join strategy

  1. each concret class maped to its own table, parent class has its own class
  2. hard to construct sub-class, as need info from parent table
  3. need @DiscriminatorColumn(name="EMP_TYPE", discriminatorType=DiscriminatorType.INTEGER)

table per concret class

 

Hints

Named Query result will be managed in persistent context, when its synchronized with transaction, changes will be persisted. So the entity must match database, otherwise, it will overwrite database when transaction commits.

 

directly modify database table is discaraged as it will make managed entity inconsisten with data in table.

 

Refresh entity state

  • refresh only applys to when entity is managed
  • if eneity is not managed, then an IllegalArgumentException will be thrown

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics