I have a class Person
and a class hierarchy which has three classes, when I use InheritanceType.TABLE_PER_CLASS for the class hierarchy, it got the "Batch update" problem. Please see the below code. AbstractBase
is the base class for the class hierarchy.
public class Person {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
@OneToMany
@JoinColumn(name="Employee_employees_Person", referencedColumnName = "id")
@Cascade({org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.MERGE})
private Set<Employee> employees = new HashSet<>();
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@javax.persistence.Access(javax.persistence.AccessType.FIELD)
public abstract class AbstractBase {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
@Entity
@Table(name = "EmployeeClass")
@javax.persistence.Access(javax.persistence.AccessType.FIELD)
public class Employee extends AbstractBase implements Serializable {
private String name;
@Column(name="Employee_employees_Person")
private String Employee_employees_Person_ID;
}
@Entity
public class Manager extends Employee {
@Property(name="ManagerLevel")
@Column(name="ManagerLevel")
private String level;
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
Test code:
em.getTransaction().begin();
Person a = new Person();
Employee e1 = new Employee();
e1.setName("Bill");
Manager m1 = new Manager();
m1.setName("Amanda");
m1.setLevel("VP");
a.getEmployees().add(e1);
a.getEmployees().add(m1);
em.persist(a);
em.getTransaction().commit();
Output:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1"
I found that Hibernate doesn't update the Manager
table when I went through the hibernate sql. When I changed the InheritanceType to InheritanceType.JOINED
, the code works well. Do I miss something in the unidirectional One-To-Many configuration?
----------------------------------update--------------------------------
In 'Person' class, I changed @JoinColumn to @JoinTable in One-To-Many annotation, the testing code works well. Is there any missed configuration for @JoinColumn configuration when its target class is annotated by "InheritanceType.TABLE_PER_CLASS"?
See below detailed annotation:
@OneToMany
//@JoinColumn(name="Employee_employees_Person", referencedColumnName = "id")
@JoinTable (
name = "Person_employees_Employee",
joinColumns={ @JoinColumn(name="Person_ID", referencedColumnName="id") },
inverseJoinColumns={ @JoinColumn(name="Employee_ID", referencedColumnName="id", unique=true)}
)
@Cascade({org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.MERGE})
private Set<Employee> employees = new HashSet<>();
-------------------------------------update--------------------------------
I tried the code with Hibernate 5.2.5 Final, the issue still exists.
Maybe it's related to HHH-9864 . Try with Hibernate 5.2.5 and see if it works better.
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.