To get more familiar with the new Java EE 6 System I have created a little demo application that should be able to store some user input in a postgres database.
I am using:
- glassfish 3.1
- Postgres 9.1
- OpenJPA 2.1.1
For this purpose I wrote following entity:
@Entity
public class User implements Serializable
{
@Id
@GeneratedValue(generator="user_seq",strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="user_seq", sequenceName="user_seq",allocationSize=1)
public Long id;
The database is completely empty and the connection pool is configured in glassfish. My persistence.xml looks like this:
<persistence-unit name="myDatabase" transaction-type="JTA" >
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>jdbc/myDatabase</jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
<property name="openjpa.jdbc.DBDictionary" value="postgres" />
</properties>
</persistence-unit>
Due to the fact, that I dont want to insert all db-objects myself, I activated the automatic schema creation in my jpa properties. With the first start openjpa (or maybe postgre) creates a new sequence named 'user_seq' as mentioned in the annotations of the entity. This works fine. But then openjpa wants to create the table 'user' and throws following exception:
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException:
FEHLER: Syntaxfehler bei »User«
Position: 14 {stmnt 31631786 CREATE TABLE User
(id BIGINT NOT NULL, userlastlogin VARCHAR(255), username VARCHAR(255),
userpassword VARCHAR(255), PRIMARY KEY (id))} [code=0, state=42601]
The SQL-Statement seems to be fine. If I create the table 'user' myself, I get the ReportingSQLException that openjpa is not able to insert the entity in the existing table.
First I thought that openjpa and postgre both wants to create the sequence with activated schema-creation. So changed the property to:
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(Sequences=false)" />
Unfortunately this did not work either.
What else can I say.. Hm.. My DAO looks like this:
@Stateless
public class DAOService
{
@PersistenceContext
protected EntityManager em;
public <T> T create(T t)
{
this.em.persist(t);
this.em.flush();
this.em.refresh(t);
return t;
}
Thanks for your help.
Perhaps 'User' is a reserved keyword on postgres? Can you try specifying the name of your Entity to be User0 or something?