hibernate annotation对jpa主键one-to-one映射关系支持的有问题 HibernateJPAJBoss
程序员文章站
2024-03-14 08:39:04
...
我们都知道在hibernate中有个foreign的生成主键的方式,这个功能主要是用在主键关联的one-to-one关系,并且我在用hbm文件中配过这种方式,在ejb3/jpa规范中也提供了一个相似功能的annotation的:PrimaryKeyJoinColumn。但是,我在用hibernate中使用这种方式的映射时,发现不起作用,也就是使被影射的对象不会自动获得ID,具体可以参看下面这个示例。
业务逻辑:系统中有多个Employee,每个Employee可以建一个且只能建一个系统用户User,但并不是每个Employee都有User,这里的就是典型的主键映射的one-to-one的关系
具体代码:
Employee.java
User.java
业务逻辑:系统中有多个Employee,每个Employee可以建一个且只能建一个系统用户User,但并不是每个Employee都有User,这里的就是典型的主键映射的one-to-one的关系
具体代码:
Employee.java
@Entity
@Table(name = "T_EMPLOYEE")
public class Employee implements Serializable {
private String id;
private User user;
public Employee() {}
@Id
@GenericGenerator(name = "generator", strategy = "uuid")
@GeneratedValue(generator = "generator")
@Column(name = "ID")
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@OneToOne(mappedBy = "employee")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
@Table(name = "T_EMPLOYEE")
public class Employee implements Serializable {
private String id;
private User user;
public Employee() {}
@Id
@GenericGenerator(name = "generator", strategy = "uuid")
@GeneratedValue(generator = "generator")
@Column(name = "ID")
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@OneToOne(mappedBy = "employee")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
User.java
@Entity
@Table(name = "T_USER")
public class User implements Serializable {
private String id;
private Employee employee;
public User() {
}
//利用hibernate提供的foreign功能来生成主键方式,这在创建User对象的时候就不需要手动设置User的ID
@Id
@GenericGenerator(name = "generator",
strategy = "foreign",
parameters = {
@Parameter(name = "property", value = "employee")
})
@GeneratedValue(generator = "generator")
@Column(name = "ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.PERSIST)
@PrimaryKeyJoinColumn
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
@Table(name = "T_USER")
public class User implements Serializable {
private String id;
private Employee employee;
public User() {
}
//利用hibernate提供的foreign功能来生成主键方式,这在创建User对象的时候就不需要手动设置User的ID
@Id
@GenericGenerator(name = "generator",
strategy = "foreign",
parameters = {
@Parameter(name = "property", value = "employee")
})
@GeneratedValue(generator = "generator")
@Column(name = "ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@OneToOne(cascade = CascadeType.PERSIST)
@PrimaryKeyJoinColumn
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
由于@PrimaryKeyJoinColumn不起作用,我只能用在User的ID上使用foreign主键生成器
在hibernate的官方jira上也有人提过这个问题(http://jira.jboss.com/jira/browse/HIBERNATE-73),不过好像没人响应,大家可以去看看
以上环境是:hibenrate3.2.4.sp1,hibernate-annotations-3.3.0.GA