当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Hibernate can't read many to many mapping

问题描述:

I have a many to many mapping: users and rooms, as follow:

User.java

@Entity

@Table(name="users", indexes = {

@Index(unique=true, columnList="uid"),

@Index(unique=true, columnList="backendAccessToken"),

@Index(columnList="backendAccessToken", name="idx_backend")

})

public class User extends BasePersistable {

private static final long serialVersionUID = 1492535311821424305L;

@Column(nullable=false, unique=true)

private String nickname;

@Column(nullable=false)

private Integer uid;

@Column(nullable=false)

private String backendAccessToken;

@Column

private String name;

@Column

@JsonIgnore

private String email;

@Column

private String location;

@Column

private String company;

@Column

private String avatar;

@Column

@JsonIgnore

private String accessToken;

@CreationTimestamp

private Date memberSince;

@ManyToMany(targetEntity=Room.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE })

@JoinTable(name="room_users",

joinColumns={ @JoinColumn(name="user_id") },

inverseJoinColumns={ @JoinColumn(name="room_id") })

private List<Room> rooms = new ArrayList<>();

Room.java

@Entity

@Table(name="rooms", indexes = {

@Index(unique=true, columnList="uid"),

@Index(columnList="uid"),

@Index(columnList="fullName")

})

public class Room extends BasePersistable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)

private Long id;

@Column(nullable=false)

private Integer uid;

@Column(nullable=false)

private String name;

@Column(nullable=true)

private String fullName;

@Column(nullable=false)

@Lob

private String description;

@Column(nullable=true)

private String homepage;

@Column(nullable=false)

private String owner;

@ManyToOne

private Organization organization;

@OneToMany(mappedBy="room")

@JsonIgnore

private List<Message> messages = new ArrayList<>();

@ManyToMany(mappedBy="rooms", targetEntity=User.class)

@JsonIgnore

private List<User> users = new ArrayList<>();

@Column(name="private")

private Boolean _private = false;

And when I try to create the schema, I'm seeing this error:

A Foreign key refering com.models.Room from com.models.User has the wrong number of column. should be 2

I did some research, tried to use JoinColumns annotation on User and didn't worked.

EDIT

I found the error: the class BasePersistable already defined the id. So I removed the id from Room and it worked. Thanks for the tip Mateusz Korwel and kakashihatake

分享给朋友:
您可能感兴趣的文章:
随机阅读: