在OneTOMany中,映射外键在Hibernate-MySQL中作为null插入

时间:2013-12-19 04:09:26

标签: java hibernate

我正在使用Hibernate 4.3MySQL

有两个表1. User2. Vehicle

我使用primary key vehicleforeign 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}}。 感谢您的帮助。

2 个答案:

答案 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