gpt4 book ai didi

java - Hibernate - @ManyToOne 注释创建附加列

转载 作者:行者123 更新时间:2023-11-28 23:43:10 25 4
gpt4 key购买 nike

所以我正在学习有关 hibernate 的教程,因为我想在即将进行的项目中使用该框架。我在使用 @ManyToOne 和 @OneToMay 注释以及双向映射时遇到问题。我想做的是非常基本的东西。我只想要两张表,一张代表用户,另一张代表车辆。现在我希望用户能够拥有许多车辆,并且汽车应该“知道”它所属的用户。

因为创建一个额外的表来保存用户和车辆之间的连接似乎是“最佳实践”。我尝试使用 @JoinTable 批注(见下文)来执行此操作,但它无法正常工作。

为此,我编写了以下类:

UserDetails.java

@Entity
@Table(name="USER_DETAILS")
public class UserDetails {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
private String userName;
private String email;
@OneToMany()
@JoinTable(name="USER_VEHICLE", joinColumns=@JoinColumn(name="USER_ID"),
inverseJoinColumns=@JoinColumn(name="VEHICLE_ID")
)
private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();

...
getters and setters

车辆.java

@Entity
public class Vehicle {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int vehicleId;
private String vehicleName;
@ManyToOne
private UserDetails user;

...
getters and setters

测试上面的类:

HibernateTest.Java

public class HibernateTest {
/**
*
* @param args
*/
public static void main(String[] args){

UserDetails user1 = new UserDetails();
user1.setUserName("testUser1");
user1.setEmail("testEmail1");


Vehicle vehicle1 = new Vehicle();
vehicle1.setVehicleName("Car");

Vehicle vehicle2 = new Vehicle();
vehicle2.setVehicleName("Jeep");

user1.getVehicle().add(vehicle1);
user1.getVehicle().add(vehicle2);
vehicle1.setUser(user1);
vehicle2.setUser(user1);

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user1);
session.save(vehicle1);
session.save(vehicle2);
session.getTransaction().commit();
session.close();

当我运行它时,它执行时没有任何错误或异常,并创建了下表:

USER_DETAILS
+------+----------+---------+
|userId|email |userName |
+------+----------+---------+
|1 |testEmail1|testUser1|
+------+----------+---------+

VEHICLE
+---------+-----------+-----------+
|vehicleId|vehicleName|user_userId| <- did not expect this column
+---------+-----------+-----------+
|1 |Car |1 |
+---------+-----------+-----------+
|2 |Jeep |1 |
+---------+-----------+-----------+

USER_VEHICLE
+------+----------+
|USER_ID|VEHICLE_ID|
+-------+----------+
|1 |1 |
+-------+----------+
|1 |2 |
+-------+----------+

据我了解,由 hibernate 创建的附加列“user_userId”似乎是不必要的,因为用户和车辆之间的连接已经在“USER_VEHICLE”表中表示,我不打算再增加这个专栏已创建。

是不是我做双向映射的方式不对?在不创建附加列的情况下使其工作的正确方法是什么?

最佳答案

如果你想要一个@ManyToMany 关系,

@JoinTable 是必需的。

@Entity
public class UserDetails {
/* ... */

@ManyToMany
@JoinTable(name = "user_vehicle",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "vehicle_id"))
private List<Vehicle> vehicles;
}

@Entity
public class Vehicle {
/* ... */
}
USER_DETAILS      VEHICLE           USER_VEHICLE
+---------+ +------------+ +---------+------------+
| user_id | | vehicle_id | | user_id | vehicle_id |
+---------+ +------------+ +---------+------------+
| 1 | | 1 | | 1 | 1 |
+---------+ +------------+ +---------+------------+

因为你不想这样,你可以像这样删除 @JoinTable:

@Entity
public class UserDetails {
/* ... */

@OneToMany(mappedBy = "user")
private List<Vehicle> vehicles;
}

@Entity
public class Vehicle {
/* ... */

@ManyToOne
private UserDetails user;
}
USER_DETAILS      VEHICLE       
+---------+ +------------+---------+
| user_id | | vehicle_id | user_id |
+---------+ +------------+---------+
| 1 | | 1 | 1 |
+---------+ +------------+---------+

如果您想为 (UserDetails, Vehicle) 元组存储额外数据,您只需要第三个表,在这种情况下,您将创建第三个类。

@Entity
public class UserDetails {
/* ... */

@OneToMany(mappedBy = "user")
private List<UserVehicle> vehicles;
}

@Entity
public class Vehicle {
/* ... */
}

@Entity
public class UserVehicle {
/* ... */

@ManyToOne
private UserDetails user;

@ManyToOne
private Vehicle vehicle;

@Basic
private String someString;
}
USER_DETAILS      VEHICLE           USER_VEHICLE
+---------+ +------------+ +---------+------------+-------------+
| user_id | | vehicle_id | | user_id | vehicle_id | some_string |
+---------+ +------------+ +---------+------------+-------------+
| 1 | | 1 | | 1 | 1 | Hello |
+---------+ +------------+ +---------+------------+-------------+

关于java - Hibernate - @ManyToOne 注释创建附加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34026195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com