我正在练习Hibernate,我正在将数据填充到数据库中,但是有一个额外的列正在与JoinColumn沿着添加
实体:移动的类-
@Entity
public class Mobile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer mobileId;
@Basic
private String mobileName;
@OneToOne(cascade = CascadeType.ALL)
private Sim sim;
public Mobile() {
super();
// TODO Auto-generated constructor stub
}
public Mobile(Integer mobileId, String mobileName, Sim sim) {
super();
this.mobileId = mobileId;
this.mobileName = mobileName;
this.sim = sim;
}
public Integer getMobileId() {
return mobileId;
}
public void setMobileId(Integer mobileId) {
this.mobileId = mobileId;
}
public String getMobileName() {
return mobileName;
}
public void setMobileName(String mobileName) {
this.mobileName = mobileName;
}
public Sim getSim() {
return sim;
}
public void setSim(Sim sim) {
this.sim = sim;
}
}
public class String {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer simId;
@Column(name="name")
private String simName;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "mobile")
private Mobile mobile;
public Sim() {
super();
// TODO Auto-generated constructor stub
}
public Sim(Integer simId, String simName, Mobile mobile) {
super();
this.simId = simId;
this.simName = simName;
this.mobile = mobile;
}
public Integer getSimId() {
return simId;
}
public void setSimId(Integer simId) {
this.simId = simId;
}
public String getSimName() {
return simName;
}
public void setSimName(String simName) {
this.simName = simName;
}
public Mobile getMobile() {
return mobile;
}
public void setMobile(Mobile mobile) {
this.mobile = mobile;
}
}
Main Class-
public class Main { public static void main(String[] args){ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Mobile mobile = new Mobile();
mobile.setMobileName("Nokia");
Sim sim = new Sim();
sim.setSimName("Aircel");
mobile.setSim(sim);
sim.setMobile(mobile);
Session session = sessionFactory.openSession();
session.save(mobile);
Transaction tx = session.beginTransaction();
tx.commit();
session.close();
sessionFactory.close();
}
}
Output:
mobile table:
mobileId|mobileName|sim_simId|
--------+----------+---------+
2|Nokia | 2|
sim table:
simId|name |mobile_mobileId|mobile|
-----+------+---------------+------+
2|Aircel| | 2|
Why is the column mobile_mobileId being generated?
1条答案
按热度按时间svgewumm1#
发生这种情况是因为你没有使用mappedBy