简体   繁体   中英

Hibernate: ManytoMany mapping: “Two-way”

I have two class:

Order.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;
    }
}

And this is the 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));

What is the problem with the two mapping? It is works for me. But I don't want to use, if it isn't the right way. If I am change the second mapping to "@ManyToMany(mappedBy="room")" (without the joinTable annotation) in the Order class. I can't list all rooms (with their orders), because I got this error message:

"Failed to lazily initialize a collection of role: com.room.model.Room.orders, could not initialize proxy - no Session"

What is the right way to can list all orders with their rooms and all rooms with their orders. So I need "two-way" join.

There are many errors in the code you posted.

First of all, your join table is not correctly defined. It shouldn't have a seperate column for the ID: noone will ever populate this column. It should be defined as

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

Second, your mapping is wrong. A bidirectional association always has an owner side, which defines how the association is mapped, and an inverse side, which uses the mappedBy attribute to tell JPA that this is the inverse side, and where to find the owner side. So, in Room, the association should be mapped with

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

And finally, your mapping for the association doesn't match with the column names in your table:

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

There is no column "roomNumber" in the table. The column is named "roomNO".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM