@GenerateValue(strategy = GenerationType.AUTO) 没有按预期工作

发布于 2024-10-19 06:48:01 字数 1154 浏览 1 评论 0原文

我正在尝试将对象持久保存到数据库中。不断收到“列 ID 无法接受空值错误”。我的对象看起来像这样:

    @Entity
public class TestTable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id = 0;

    @Column(nullable=false, length=256)
    private String data = "";

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}

我的持久函数:

public static synchronized boolean persistObject(Object obj){
        boolean success = true;
        EntityManager em = null;
        EntityTransaction tx = null;
        try{
            em = getEmf().createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            em.persist(obj);
            tx.commit();

        } catch (Exception e){
            success = false;
        } finally{
            try{
                em.close();
            } catch(Exception e){
                //nothing
            }
        }
        return success;
    }

I'm trying to persist an object to a database. Keep getting 'Column ID cannot accept null value error'. My object looks like this:

    @Entity
public class TestTable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id = 0;

    @Column(nullable=false, length=256)
    private String data = "";

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}

My persist function:

public static synchronized boolean persistObject(Object obj){
        boolean success = true;
        EntityManager em = null;
        EntityTransaction tx = null;
        try{
            em = getEmf().createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            em.persist(obj);
            tx.commit();

        } catch (Exception e){
            success = false;
        } finally{
            try{
                em.close();
            } catch(Exception e){
                //nothing
            }
        }
        return success;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

七月上 2024-10-26 06:48:01

您可以使用 GenerationType.TABLE。
这样,jpa 使用序列表进行 id 分配,您可能永远不需要生成序列或自动增量值或降低可移植性的触发器。

另请注意,在 java 中 int 类型默认以 0 启动,因此您也可以摆脱它。

You may use GenerationType.TABLE.
That way, jpa uses a sequence table for id assigment and you may never need to generate sequence or auto-increment values or triggers that lowers portability.

Also note that in java int type is initiated with 0 default, so you may get rid of that also.

鸢与 2024-10-26 06:48:01

就我而言,

hibernate.dialect=org.hibernate.dialect.H2Dialect

这是关于错误的方言:而不是

hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect

当我切换到生产数据库时, :。 Hibernate 尝试使用为不同数据库引擎准备的策略。

In my case it was about bad dialect:

hibernate.dialect=org.hibernate.dialect.H2Dialect

instead of:

hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect

when I switched to the production database. Hibernate tried to use strategy prepared for different db engine.

徒留西风 2024-10-26 06:48:01

我遇到了与你类似的问题。我最终发现我的数据库连接的配置是错误的:我连接到一个具有不正确架构的旧数据库。新架构将主键列声明为

“ID”BIGINT NOT NULL 始终作为 IDENTITY 生成(从 1 开始,按 1 递增)

所以 数据库本身自动生成主键,而旧模式将其声明为

“ID”整数非空

Hibernate 为新模式执行了正确的代码,但在旧模式上失败了,因为旧模式要求 SQL INSERTINSERT 提供一个值代码>ID列。

I had a problem with a similar manifestation to yours. I eventually discovered that the configuration of my database connection was wrong: I was connecting to an old database that had an incorrect schema. The new schema declared the primary-key column as

"ID" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)

so the database itself automatically generated the primary key whereas the old schema declared it as

"ID" INTEGER NOT NULL

Hibernate executed the correct code for the new schema, which failed on the old schema because the old schema demanded the SQL INSERT provide a value for the ID column.

一影成城 2024-10-26 06:48:01

以防万一有人遇到与我相同的问题。我已经为此苦苦挣扎了几个小时,只想发布我的解决方案。看起来 ID 总是设置为 0,这会引发错误。

模型中的 id 属性必须是“Long”或“Integer”类型,而不是“long”或“integer”类型(注意大小写)。我输入了“long”,它是一个原始类型,不能为空。因此默认值为 0,Hibernate 没有意识到它是一个新实体,并尝试使用 ID = 0 来保存它,这显然不起作用。

将其更改为引用类型“Long”(可以为空),解决了它。

Just in case someone arrives at this thread with the same problem as me. I have been struggling with this for some hours now, and just want to post my solution. It seemed the ID was always set to 0, and that threw an error.

The id property in the model must be of type "Long" or "Integer" and not "long" or "integer" (notice the capitalization). I had put "long", which is a primitive type and cannot be null. Therefore the default value was 0 and Hibernate didn't realize that it was a new entity and tried to save it with ID = 0, something that obviously did not work.

Changing this into the reference type "Long", which can be null, solved it.

对你而言 2024-10-26 06:48:01

或者尝试使用 @GenerateValue(strategy = GenerationType.AUTO)
而不是@GenerateValue(strategy = GenerationType.SEQUENCE)。

Or try with @GeneratedValue(strategy = GenerationType.AUTO)
instead of @GeneratedValue(strategy = GenerationType.SEQUENCE).

初懵 2024-10-26 06:48:01

当 ID 列是 Int 时,Hibernate 以无声且神秘的方式失败。尝试在代码中将其更改为 Long,并在数据库中将其更改为无符号 64 位整数。这为我解决了这个问题。

Hibernate fails in silent and mysterious ways when the ID column is an Int. Try changing it to Long in the code and an unsigned 64-bit integer in the database. That fixed the issue for me.

↙温凉少女 2024-10-26 06:48:01

我正在努力解决同样的问题,不知何故,我需要长时间工作而不是长类型:)。作为解决方法,我将数据库中的 id 列转换为 SERIEL。我正在使用 postgres 数据库。

I was struggling with the same problem and somehow for me long work instead of LONG type :). As a workarround I converted id column in database as SERIEL. I am using postgres db.

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