MySql JPA toplink Auto increment id generation.

To configure JPA entity to make use of auto increment feature of MySql for primary key generation you can make use of IDENTITY strategy ref below code. @Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; . . . public long getId() { return id; } } But problem with this way of generating ids with MySql and JPA combination is when you persist new entity and try to retrieve id assigned to newly persisted entity using getId() method. [Read More]
jpa  mysql  ORM  Java 

Override JPA persistence unit properties in code with Google Guice

To override properties from persistence.xml through code while using it with Google guice. One can read the properties from different source and add those properties while installing JpaPersistModule. Following is the sample Properties persistenceUnit = new Properties(); persistenceUnit.put("javax.persistence.jdbc.url", "jdbc:mysql://192.168.9.102/MyDB"); persistenceUnit.put("javax.persistence.jdbc.user", "myuser"); persistenceUnit.put("javax.persistence.jdbc.password", "mypassword"); Injector injector = Guice.createInjector(new JpaPersistModule("my-persistence-unit").properties(persistenceUnit)); One can chose to read this configuration from some other file and install JpaPersistModule with those properties. This could be useful to override database url for test and production. [Read More]