小编Luc*_*cao的帖子

Hibernate inheritance, polymorphic saving

I have a class Person with two subclasses Student and Employee

@Entity(name="person")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {
    @Id
    @GeneratedValue
    private Integer id;
}

@Entity(name="student")
@PrimaryKeyJoinColumn(name="id")
public class Student extends Pessoa implements Serializable {
}

@Entity(name="employee")
@PrimaryKeyJoinColumn(name="id")
public class Employee extends Person implements Serializable {
}
Run Code Online (Sandbox Code Playgroud)

A student can also be an employee, but when I try to save a student with the same id as an employee, hibernate throws a duplicate primary key exception

org.hibernate.exception.ConstraintViolationException: Duplicate …
Run Code Online (Sandbox Code Playgroud)

java sql inheritance hibernate

5
推荐指数
1
解决办法
818
查看次数

标签 统计

hibernate ×1

inheritance ×1

java ×1

sql ×1