OpenJPA 无法在 Postgres DB 中创建/插入表
为了更熟悉新的 Java EE 6 系统,我创建了一个小演示应用程序,它应该能够在 postgres 数据库中存储一些用户输入。
我正在使用:
- glassfish 3.1
- Postgres 9.1
- OpenJPA 2.1.1
为此,我编写了以下实体:
@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;
数据库完全为空,连接池在 glassfish 中配置。我的 persistence.xml 如下所示:
<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>
由于我不想自己插入所有数据库对象,因此我在 jpa 属性中激活了自动模式创建。第一次启动时,openjpa(或者可能是 postgre)会创建一个名为“user_seq”的新序列,如实体注释中所述。 这很好用。但随后 openjpa 想要创建表“user”并抛出以下异常:
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]
SQL 语句似乎没问题。如果我自己创建表“user”,我会收到 ReportingSQLException,表明 openjpa 无法在现有表中插入实体。
首先,我认为 openjpa 和 postgre 都想通过激活的模式创建来创建序列。因此将属性更改为:
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(Sequences=false)" />
不幸的是,这也不起作用。
我还能说什么.. 嗯.. 我的 DAO 看起来像这样:
@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;
}
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许“User”是 postgres 上的保留关键字?您可以尝试将实体名称指定为 User0 或其他名称吗?
Perhaps 'User' is a reserved keyword on postgres? Can you try specifying the name of your Entity to be User0 or something?