简体   繁体   中英

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.

@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

@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. Does someone kwows whats going on? im using SQL Server 2008 and hibernate 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. 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

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;
    [...]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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