我正在使用Hibernate 4.3
和MySQL
有两个表1. User
和2. Vehicle
我使用primary
key
vehicle
在foreign key
中插入了用户的hibernate
annotation
,使用了以下代码中显示的mapping
@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {
@Id
@Column (name="USER_ID")
private int userId;
@Column (name="USER_NAME")
private String userName;
@OneToMany(mappedBy="user")
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();
public int getUserId() {
return userId;
}
public Collection<Vehicle> getVehicle() {
return vehicle;
}
public void setVehicle(Collection<Vehicle> vehicle) {
this.vehicle = vehicle;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
@Entity
public class Vehicle {
@Id
private int vehicleId;
private String vehicleName;
@ManyToOne
@JoinColumn(name="USER_ID")
private UserDetails user;
public UserDetails getUser() {
return user;
}
public void setUser(UserDetails user) {
this.user = user;
}
public String getVehicleName() {
return vehicleName;
}
public void setVehicleName(String vehicleName) {
this.vehicleName = vehicleName;
}
public int getVehicleId() {
return vehicleId;
}
public void setVehicleId(int vehicleId) {
this.vehicleId = vehicleId;
}
}
用户类
public class HibernateMain {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName("Second user");
Vehicle vehicle = new Vehicle();
vehicle.setVehicleId(1);
vehicle.setVehicleName("car");
Vehicle vehicle1 = new Vehicle();
vehicle1.setVehicleId(2);
vehicle1.setVehicleName("jeep");
user.getVehicle().add(vehicle);
user.getVehicle().add(vehicle1);
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
Session session = sf.openSession();
session.beginTransaction();
session.save(user);
session.save(vehicle);
session.save(vehicle1);
session.getTransaction().commit();
session.close();
}
}
车辆类
mysql> select * from user_details;
+---------+-------------+
| USER_ID | USER_NAME |
+---------+-------------+
| 1 | Second user |
+---------+-------------+
mysql> select * from vehicle;
+-----------+-------------+---------+
| vehicleId | vehicleName | USER_ID |
+-----------+-------------+---------+
| 1 | car | NULL |
| 2 | jeep | NULL |
+-----------+-------------+---------+
主要课程
User_ID
输出:
vehicle
为什么null
表中的1
正在{{1}}。它应该是{{1}}。
感谢您的帮助。
答案 0 :(得分:4)
在保存车辆对象之前,您需要明确说出vehicle.setUser
。
答案 1 :(得分:1)
更改
Session session = sf.openSession();
session.beginTransaction();
session.save(user);
session.save(vehicle);
session.save(vehicle1);
session.getTransaction().commit();
session.close();
到
Session session = sf.openSession();
session.beginTransaction();
session.save(vehicle);
session.save(vehicle1);
user.getVehicle().add(vehicle);
user.getVehicle().add(vehicle1);
session.save(user);
session.getTransaction().commit();
session.close();
从此处删除关联
vehicle1.setVehicleId(2);
vehicle1.setVehicleName("jeep");
user.getVehicle().add(vehicle); <--- remove
user.getVehicle().add(vehicle1); <--- remove