简体   繁体   中英

Hibernate doesn't update or delete, but does insert

I'm working on an existing database and have created Hibernate entities. The problematic entity is OrderHeader , which has a list of OrderLine . Here is my mapping:

@Entity
@Table(name = "orderline")
public class OrderLine implements Serializable {

@Id
@Column(name = "OrderLineID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer orderLineID;

@ManyToOne
@NotNull
OrderHeader orderHeader;
}

...

@Entity
@Table(name = "orderheader")
public class OrderHeader implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "OrderHeaderID")
int orderHeaderId;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
List<OrderLine> orderLineList;
}

All operations performed are on detached entities. When i use session.save to insert a new OrderHeader with a list of OrderLine everything executes perfectly. However, upon calling session.update on a detached, altered OrderHeader the only SQL statement being executed is the select statement retrieving the result of the update (which didn't happen in the first place). It gets even worse when I try to delete OrderHeader - despite having set CascadeType.ALL and orphanRemoval = true on the join upon calling session.delete hibernate is updating OrderLine s in the list by setting the OrderHeaderID as null, which throws an exception.

The DDL for both tables contain just plain data definitions with a single simple primary key. Every single DAO operation is performed in an invidual session.

Could it be that I'm missing something? The relationship annotations seem ok, I'm pretty sure I'm using the right methods to access the database (although I've tried every one out there). I've literally ran out of options. Any advice is welcome, thanks

EDIT: Updated the definitions, now I get ERROR org.hibernate.util.JDBCExceptionReporter - Table 'dbname.orderheader_orderline' doesn't exist

1 - Your problem is clearly the @OneToMany mapping which should look like this :

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy="orderHeader")
List<OrderLine> orderLineList;
  • the mappedBy value to tell JPA that it is a bidirectionnal relation
  • no @JoinColumn : it has already been set in the @ManyToOne

2 - that said, keep the cascading and stuff only on the @OneToMany and remove :

(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

from the @ManyToOne ..

EDIT here is what your code should look like : @ManyToOne with the @JoinColumn ... and @OneToMany with the property mappedBy

@Entity
@Table(name = "orderline")
public class OrderLine implements Serializable {

@Id
@Column(name = "OrderLineID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer orderLineID;

@ManyToOne
@JoinColumn(name = "OrderHeaderID")
OrderHeader orderHeader;
}

...

@Entity
@Table(name = "orderheader")
public class OrderHeader implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "OrderHeaderID")
int orderHeaderId;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy="orderHeader")
List<OrderLine> orderLineList;
}

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