简体   繁体   English

Hibernate Insert正在尝试更新标识列

[英]Hibernate Insert is trying to update identity column

im new with Hibernate and i have some issues when trying to persist an object in the database. 我是Hibernate的新手,尝试在数据库中保留对象时遇到一些问题。

@Entity
@Table(name="order")
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long number;

@Column(name="date")
private Timestamp date;

@Column(name="obs")
private String obss;

@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinColumn(name="id", referencedColumnName="number")
private List <OrderDetail> orderDetail;

(...)

and the OrderDetail Class 和OrderDetail类

@Entity
@Table(name="orderDetail")
public class OrderDetail implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO) 
private Long id;

@Column(name="orderType")
private Long orderType;

(...)

When i try to save objects into DB i get the following ERROR: 当我尝试将对象保存到数据库时,出现以下错误:

Hibernate: 
    insert 
    into
        order
        (date, obs, rejectObs, pid, destiny, stateId, userId, version) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        OrderDetail
        (boxId, date, digitalid, documentId, rejectObs, obs, orderId, stateId, orderType) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?,)
Hibernate: 
    update
        OrderDetail
    set
        id=? 
    where
        id=?
16/09/2014 11:50:03 org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 8102, SQLState: S1000
16/09/2014 11:50:03 org.hibernate.util.JDBCExceptionReporter logExceptions
ERROR: Cannot update identity column 'id'.
16/09/2014 11:50:03 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
GRAVE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not insert collection: [com.sa.ra.entities.Order.orderDetail#4252]

I dont know why Hibernate is trying to update the orderDetail table. 我不知道为什么Hibernate试图更新orderDetail表。 Does someone kwows whats going on? 有人知道发生了什么吗? im using SQL Server 2008 and hibernate 3.5.0 即时通讯使用SQL Server 2008和休眠3.5.0

There's a concept error. 有一个概念错误。

You are asking hibernate to put in reference two entities using their id as both primary key and foreign key of each other. 您要让hibernate引用两个实体,并将它们的ID用作彼此的主键和外键。 This is wrong. 错了

The correct way to put two entities in a one-to-many relation is to add a foreign key in the "many" side, that have to reference the primary key of the "one" class. 将两个实体建立一对多关系的正确方法是在“许多”侧添加一个外键,该外键必须引用“一个”类的主键。 It's a database matter, it's not only an hibernate restriction. 这是数据库问题,不仅是休眠限制。

You can read more about Hibernate Collections here 您可以在此处阅读有关Hibernate Collections的更多信息。

According to the above documentation, you have to modify your code as follows: 根据上述文档,您必须按如下方式修改代码:

The "one" side “一个”方面

@Entity
@Table(name="order")
public class Order implements Serializable {
    [...]
    @OneToMany(targetEntity = OrderDetail.class, cascade = CascadeType.ALL, mappedBy = "order", fetch = FetchType.LAZY)
    private List <OrderDetail> orderDetail;
    [...]
}

Then, the "many" side: 然后,“许多”方面:

@Entity
@Table(name="orderDetail")
public class OrderDetail implements Serializable {
    [...]
    @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
    @JoinColumn(name = "order_id", nullable = false)
    private Order order;
    [...]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM