我正在使用带有注释的休眠,并且我创建了两个实体,一个名为用户,另一个是聊天室。我想定义双向多对多映射。但是在一个实体中,它不允许我应用多对多注释并给出错误“多对多映射属性值类型不应该是持久实体”这里是实体
package com.ciit.onlineexam.model;
import com.ciit.onlineexam.util.Mailer;
import org.apache.commons.io.FileUtils;
import javax.jws.soap.SOAPBinding;
import javax.persistence.*;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
@Entity
@Table(name="users")
public class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id_users")
public int getId_user() {
return id_user;
}
private int id_user;
public void setId_user(int id_user) {
this.id_user = id_user;
}
@Column(name = "email")
private String email;
@Column(name="password")
private String password;
@Column(name = "name")
private String name;
private String reg_id;
private String contact;
@Column(name="isInformed")
private boolean informed;
//Giving error on this list
@ManyToMany(cascade = CascadeType.ALL)
private List<Chatroom> chatrooms = new ArrayList<Chatroom>(0);
public User(){}
public List<Chatroom> getChatrooms() {
return chatrooms;
}
public void setChatrooms(List<Chatroom> chatrooms) {
this.chatrooms = chatrooms;
}
public boolean isInformed() {
return informed;
}
public void setInformed(boolean informed) {
this.informed = informed;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReg_id() {
return reg_id;
}
//public void setClas(Set<Clas> clas) {
// this.clas=clas;
//}
public void setReg_id(String reg_id) {
this.reg_id = reg_id;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
@OneToMany(mappedBy = "primaryKey.user",cascade = CascadeType.ALL)
public Set<ClassesStudents> getClassesStudents() {
return classesStudents;
}
private Set<ClassesStudents> classesStudents;
public void setClassesStudents(Set<ClassesStudents> classesStudents) {
this.classesStudents = classesStudents;
}
public List<User> sendMailForAccountDetails(List<User> users, String path) throws Exception {
List<User> users1 = new ArrayList<User>();
for (User user : users) {
if (!user.isInformed()) {
Mailer mailer = new Mailer();
String subject = "You have been added to AcademicsTOOLS ";
String emailContents = FileUtils.readFileToString(new File(path + "emailTemplates" + "/" + "SendAccountDetails.html"));
emailContents = emailContents.replaceAll("-AddedStudentName-", user.getName());
emailContents = emailContents.replaceAll("-emailId-", user.email);
emailContents = emailContents.replaceAll("-accountPassword-", user.getPassword());
// emailContents = emailContents.replaceAll("-ProfileLink-", path);
mailer.postMail(user.email, subject, emailContents);
user.setInformed(true);
users1.add(user);
System.out.println("Reached in email AD ==================================================");
}
}
return users1;
}
public User sendMailForAccountDetails(User user, String path) throws Exception {
List<User> users = new ArrayList<User>();
users.add(user);
sendMailForAccountDetails(users, path);
user.setInformed(true);
return user;
}
@Override
public String toString() {
return "User{" +
"id_user=" + id_user +
", email='" + email + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", reg_id='" + reg_id + '\'' +
", contact=" + contact +
'}';
}
}
其他类是聊天室
package com.ciit.onlineexam.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Entity
@Table(name = "Chatroom")
public class Chatroom {
@Column(name="Creator",nullable = false)
private int creator;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="id_chatroom")
private int id;
@Column(name="Name",nullable = false)
private String name;
@Column(name="Description")
private String description;
@Column(name = "Category")
private String category;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "userschatrooms",
joinColumns = { @JoinColumn(name = "id_chatroom",nullable = false)},
inverseJoinColumns = { @JoinColumn(name = "id_users")})
private List<User> listOfUsers = new ArrayList<User>(0);
public Chatroom(){}
public String getName() {
return name;
}
public void setName(String group_name) {
this.name = group_name;
}
public int getCreator() {
return creator;
}
public void setCreator(int group_creator) {
this.creator = group_creator;
}
public int getId() {
return id;
}
public void setId(int group_id) {
this.id = group_id;
}
public String getDescription() {
return description;
}
public void setDescription(String group_description) {
this.description = group_description;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public void setListOfUsers(List<User> listOfStudents) {
this.listOfUsers = listOfStudents;
}
public List<User> getListOfUsers() {
return listOfUsers;
}
}