让我们假设我有两个实体A和B
实体A:
@Entity
@Table(name="A")
public class A implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idA")
private int idA;
@OneToMany(fetch=FetchType.EAGER)
private List<B> listofB ;
///GETTERS AND SETTERS
}
实体B:
@Entity
@Table(name="B")
public class B implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idB")
private int idB;
///GETTERS AND SETTERS
}
现在当我运行此代码时,hibernate将自动在数据库中生成一个表。表的名称是A_B,它包含ID为A,ID为B,
*********************
* Table A_B *
*********************
*A_idA * listofB_idB*
* * *
*********************
但我想要做的实际上是将hibernate用于生成每行中带有数值的表,并且我希望在每个新行中都考虑它,我的意思是我想要一个像这样的表
*****************************
* Table A_B *
*****************************
*A_idA * listofB_idB* Value *
* * * *
*****************************
我该怎么办?
答案 0 :(得分:0)
答案是创建一个新实体C
@Entity
@Table(name="C")
public class C implements Serializable {
@Id
@Column(name="idC")
private int idC;
@Column(name="value")
private int value;
@OneToOne
private B b;
///GETTERS AND SETTERS
}
将实体A修改为
@Entity
@Table(name="A")
public class A implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="idA")
private int idA;
@OneToMany(fetch=FetchType.EAGER)
private List<C> listofC ;
///GETTERS AND SETTERS
}
然后hibernate将生成一个名为A_C
的表