我有一个班级:
@Entity
public class myClass {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
...
private String orderIndex;
}
我想将orderIndex
放在另一个表中,因为只有少数实体会拥有此属性。我希望这些表看起来像这样:
table myClass:
id | name | ...
table myClass_orderIndex:
myClass_id | orderIndex
我找不到在hibernate中执行此操作的注释
答案 0 :(得分:0)
也许一对一的关系符合您的要求。一对一由@JoinColumn(unique=true)
@Entity
@Table(name="myClass_orderIndex")
public class OtherClass {
private String orderIndex;
@ManyToOne
@JoinColumn(unique=true)
private MyClass myClass;
...
}
@Entity
public class MyClass {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy="myClass", cascade = javax.persistence.CascadeType.ALL, orphanRemoval = true)
@Cascade(CascadeType.ALL)
private List<OtherClass> list;
@Transient
public String getOrderIndex() {
if (list != null && list.size() == 1) return list.get(0).getOrderIndex();
return null;
}
}
这将根据您的要求创建两个表。