發表文章

目前顯示的是有「Persistence」標籤的文章

Optimizing the EclipseLink Application (ELUG)

Reference: http://wiki.eclipse.org/Optimizing_the_EclipseLink_Application_%28ELUG%29

EclipseLink How to Configure Primary Key Generation

Using Sequence Objects When using a database that supports sequence objects (such as Oracle Database), EclipseLink can use a database sequence object to automatically generate identifiers for your persistent objects. Using A Default Sequence EclipseLink can produce a default sequence during schema generation. If you use schema generation, then specify that your identifier should be generated and that the SEQUENCE strategy be used to perform the generation. In the following example, the @GeneratedValue annotation indicates that the identifier value should be automatically generated; a strategy of SEQUENCE indicates that a database sequence should be used to generate the identifier. EclipseLink will create a default sequence object during schema generation that will be used at run time. @ Entity public class Inventory implements Serializable {   @Id @GeneratedValue ( strategy=GenerationType. SEQUENCE ) private long id; ...

Multiple writable mappings exist for the field. Only one may be defined as writable, all others must be specified read-only.

In entity bean, the foreign key usually replaced by the referential relationship such as : public class Status implements Serializable {     /**      *      */     private static final long serialVersionUID = 1L;     @Column(name="creation_datetime", nullable = false)     private Timestamp creationDatetime;     @Id     @Column(name="seq_no", nullable = false)     @GeneratedValue(strategy=GenerationType.IDENTITY)     private Integer seqNo;     @Column(name="status", nullable = false)     private String status;     @Column(name="car_id", nullable = false, insertable=false, updatable=false)     private String carId;     @ManyToOne     @JoinColumn(name = "car_id")     private Transaction transaction;     pub...

When should we close the EntityManager and EntityManagerFactory

For EntityManager: It depends on how you obtained it.  If you created it using EntityManagerFactory you will have to close it no matter what framework you use. If you obtained it using dependency injection (eg using EJB and @PersistenceContext annotation) you should not close it by hand. It's the EntityManager that is actually associated to a database connection and closing the EntityManager will actually release the JDBC connection (most often, return it to a pool). For EntityManagerFactory: Closing an EntityManagerFactory would be closer to destroying a whole connection pool. If you want to think JDBC connection, you should think EntityManager. We should not close the factory after every operation. Creating an EntityManagerFactoryis a pretty expensive operation and should be done once for the lifetime of the application (you close it at the end of the application). So, no, you should not close it for each persist/update/delete operation. The EntityManagerFa...

JPA Caching

http://weblogs.java.net/blog/archive/2009/08/21/jpa-caching http://www.developer.com/java/ent/article.php/3892261/JPA-20-Cache-Vs-Hibernate-Cache-Differences-in-Approach.htm

ClassCastException of persistence.xml

java.lang.ClassCastException: : org.apache.xerces.parsers.XIncludeAwareParserConfiguration cannot be cast to org.apache.xerces.xni.parser.XMLParserConfiguration" occurred while attempting to determine the version of "zip:C:/JDeveloper/11.1.1.4.0/user_projects/domains/.../persistence.xml". The problem maybe caused by the encoding of the persistence.xml. Another possible reason is caused by the classloading of the xercesImpl.jar. Qwblogic has built-in xercesImpl.jar, your application should load the jar from parent classloader. However,  using org.apache.xerces.* in weblogic-application.xml may cause classloading problem.  Solution: Remove the prefer settings from weblogic-application.xml - not exactly work or exclude this jar from POM.xml. - work

ClassCastException of entity class

The problem maybe caused by the entity class has been cached in the server even you have renamed the package and redeploy the application. For example, com.service.persistence.delivery.A changed to com.service.persistence.reprint.delivery.A. private static String persistenceUnitName = "EclipseLinkWeb"; private static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName); EntityManagerFactory is acquired statically, and there is no code to free the EntityManagerFactory and other resources before the app starts up again.  This causes the underlying resources to still exist, which have links to the old classloader, and this leads to java.lang.ClassCastException after redeployment. Workaround: Please restart the server. Solution: First, I added the following method to JPAUtil class: public class JPAUtil {   ...       public static void releaseResources() {         if (emf != null) {...

Programmatic EntityManager

Instead of you dependence Injection for EntityManager by annotation, you can also adopt programmatic EntityManager by the following code. private static EntityManagerFactoryMap buildEmFactories(String name, String persistenceUnit){                 List dsList =  CsvConfigManager.getReprintConfig(name).getDataSourceList();         EntityManagerFactoryMap managerFactories = new EntityManagerFactoryMap();         for (DataSourceEntry ds: dsList) {             HashMap property = new HashMap ();             property.put(TRANSACTION_TYPE, "JTA");             property.put(JTA_DATASOURCE, ds.getJndi());             property.put(CACHE_TYPE_DEFAULT, "NONE");       ...