gpt4 book ai didi

java - Hibernate:多对多映射: "Two-way"

转载 作者:行者123 更新时间:2023-11-29 12:37:19 25 4
gpt4 key购买 nike

我有两门课:

订单.java:

@Entity
@Table(name = "orders", catalog = "ownDB")
public class Order {

private int orderNO;
private String oderName;
private Set<Room> rooms = new HashSet<Room>(0);

public Order(int orderNo, String orderName, Set<Room> rooms) {
this.oderNo = orderNo;
this.orderName = orderName;
this.rooms = rooms;
}

@Id
@Column(name = "orderNO", unique = true, nullable = false, length = 6)
public int getOrderNO() {
return this.orderNO;
}

public void setOrderNo(int OrderNO) {
this.orderNO = orderNO;
}

@Column(name = "orderName", nullable = false, length = 100)
public String getOrderName() {
return this.orderName;
}

public void setOrderName(String orderName) {
this.orderName = orderName;
}

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "rooms_orders", joinColumns = { @JoinColumn(name = "orderNO") }, inverseJoinColumns = { @JoinColumn(name = "roomNO") })
public Set<Room> getRoom() {
return this.rooms;
}

public void setRoom(Set<Room> rooms) {
this.rooms = rooms;
}
}

这是 room.java:

@Entity
@Table(name = "rooms", catalog = "ownDB")
public class {

private int roomNO;
private String name;
private Set<Order> orders = new HashSet<Order>(0);

public Room(int roomNO, String name, Set<Order> orders) {
this.roomNO = roomNO;
this.name = name;
this.orders = orders;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "roomNO", unique = true, nullable = false, length = 6)
public int getRoomNO() {
return this.roomNO;
}

public void setRoomNO(int roomNO) {
this.roomNO = roomNO;
}

@Column(name = "name", nullable = false, length = 100)
public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "rooms_orders", joinColumns = { @JoinColumn(name = "roomNO") }, inverseJoinColumns = { @JoinColumn(name = "orderNO") })
public Set<Order> getOrders() {
return this.orders;
}

public void setOrders(Set<Order> orders) {
this.orders = orders;
}

}

SQL:

CREATE  TABLE rooms (
roomNO int(6) NOT NULL ,
name VARCHAR(100) NOT NULL ,
PRIMARY KEY (roomNO));

CREATE TABLE orders (
orderNO int(6) NOT NULL AUTO_INCREMENT,
orderName VARCHAR(100) NOT NULL,
PRIMARY KEY (orderNO));

CREATE TABLE rooms_orders (
roomNO int(6) NOT NULL,
orderNO int (6) NOT NULL,
PRIMARY KEY (roomNO, orderNO));

这两个映射有什么问题?这对我有用。但如果方法不正确,我不想使用。如果我将 Order 类中的第二个映射更改为“@ManyToMany(mappedBy="room")”(没有 joinTable 注释)。我无法列出所有房间(及其订单),因为我收到此错误消息:

“无法延迟初始化角色集合:com.room.model.Room.orders,无法初始化代理 - 无 session ”

列出所有订单及其房间以及所有房间及其订单的正确方法是什么。所以我需要“双向”加入。

最佳答案

您发布的代码中有很多错误。

首先,您的连接表定义不正确。它不应该有一个单独的 ID 列:没有人会填充此列。它应该被定义为

CREATE TABLE rooms_orders (
roomNO int(6) NOT NULL,
orderNO int (6) NOT NULL,
PRIMARY KEY (rommNO, orderNO));

其次,你的映射是错误的。双向关联始终有一个所有者端(它定义关联的映射方式)和一个反向端(它使用mappedBy 属性告诉 JPA 这是反向端,以及在哪里可以找到所有者端)。因此,在 Room 中,关联应该映射为

@ManyToMany(mappedBy = "rooms")
public Set<Order> getOrders() {
return this.orders;
}

最后,您的关联映射与表中的列名称不匹配:

@JoinTable(name = "rooms_orders", 
joinColumns = { @JoinColumn(name = "orderNO") },
inverseJoinColumns = { @JoinColumn(name = "roomNumber") })

表中没有“roomNumber”列。该列名为“roomNO”。

关于java - Hibernate:多对多映射: "Two-way",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573444/

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