OpenJPA 无法在 Postgres DB 中创建/插入表

发布于 2024-12-17 03:38:48 字数 2032 浏览 5 评论 0原文

为了更熟悉新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-12-24 03:38:48

也许“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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文