我在Maven项目中使用了休眠核心,我想为以下数据库创建实体:db_schema
我刚刚开始学习休眠,所以请耐心地学习这一点。我知道这可能是一个愚蠢的错误,但我无法弄清楚。
我尝试测试代码时遇到以下异常:
org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:entity.User.groups [entity.IsIn]
我将在下面列出我的实体的代码
用户
package entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userId")
private int userId;
@OneToMany(
mappedBy = "user",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<IsIn> groups = new ArrayList<>();
@Column(name = "customerName", nullable = false)
private String customerName;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "is_active", nullable = false)
private boolean is_active;
@Column(name = "notificationType", nullable = false)
private String notificationType;
@Column(name = "create_date", nullable = false)
private String create_date;
public User() { }
public User(String customerName, String password, String email, boolean is_active, String notificationType, String create_date) {
this.customerName = customerName;
this.password = password;
this.email = email;
this.is_active = is_active;
this.notificationType = notificationType;
this.create_date = create_date;
}
public void addGroup(Group group) {
IsIn isIn = new IsIn(this, group);
groups.add(isIn);
}
public void removeGroup(Group group) {
for (Iterator<IsIn> iterator = groups.iterator();
iterator.hasNext(); ) {
IsIn isIn = iterator.next();
if (isIn.getUser().equals(this) &&
isIn.getGroup().equals(group)) {
iterator.remove();
isIn.setUser(null);
isIn.setGroup(null);
}
}
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isIs_active() {
return is_active;
}
public void setIs_active(boolean is_active) {
this.is_active = is_active;
}
public String getNotificationType() {
return notificationType;
}
public void setNotificationType(String notificationType) {
this.notificationType = notificationType;
}
public String getCreate_date() {
return create_date;
}
public void setCreate_date(String create_date) {
this.create_date = create_date;
}
}
组
package entity;
import org.hibernate.annotations.NaturalIdCache;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "group")
@NaturalIdCache
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "groupId")
private int groupId;
@Column(name = "groupName")
private String groupName;
@Column(name = "notificationMessage", nullable = false)
private String notificationMessage;
@Column(name = "create_date", nullable = false)
private String create_date;
@OneToOne
@JoinColumn(name = "created_by")
private User created_by;
@Column(name = "isPrivate")
private boolean isPrivate;
public Group() { }
public Group(String groupName, String notificationMessage, String create_date, boolean isPrivate) {
this.groupName = groupName;
this.notificationMessage = notificationMessage;
this.create_date = create_date;
this.isPrivate = isPrivate;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
Group group = (Group) o;
return Objects.equals(groupId, group.getGroupId());
}
@Override
public int hashCode() { return Objects.hash(groupId); }
public int getGroupId() {
return groupId;
}
public void setGroupId(int groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getNotificationMessage() { return notificationMessage; }
public void setNotificationMessage(String notificationMessage) {
this.notificationMessage = notificationMessage;
}
public String getCreate_date() {
return create_date;
}
public void setCreate_date(String create_date) {
this.create_date = create_date;
}
public boolean isPrivate() {
return isPrivate;
}
public void setPrivate(boolean aPrivate) {
isPrivate = aPrivate;
}
public User getCreated_by() {
return created_by;
}
public void setCreated_by(User created_by) {
this.created_by = created_by;
}
}
UserGroupId(可嵌入)
package Embedded;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
@Embeddable
public class UserGroupId implements Serializable {
@Column(name = "userId")
private int userId;
@Column(name = "groupId")
private int groupId;
private UserGroupId() {}
public UserGroupId(int userId, int groupId) {
this.userId = userId;
this.groupId = groupId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
UserGroupId that = (UserGroupId) o;
return Objects.equals(userId, that.userId) &&
Objects.equals(groupId, that.groupId);
}
@Override
public int hashCode() {
return Objects.hash(userId, groupId);
}
}
IsIn
package entity;
import Embedded.UserGroupId;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "isin")
public class IsIn {
@EmbeddedId
private UserGroupId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("userId")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("groupId")
private Group group;
@OneToOne
@JoinColumn(name = "typeId")
private UserType typeId;
@Column(name = "isBlocked", nullable = false)
private boolean isBlocked;
private IsIn() {}
public IsIn(User user, Group group) {
this.user = user;
this.group = group;
this.id = new UserGroupId(user.getUserId(), group.getGroupId());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
IsIn that = (IsIn) o;
return Objects.equals(user, that.user) &&
Objects.equals(group, that.group);
}
@Override
public int hashCode() {
return Objects.hash(user, group);
}
public User getUser() {
return user;
}
public Group getGroup() {
return group;
}
public void setUser(User user) {
this.user = user;
}
public void setGroup(Group group) {
this.group = group;
}
public UserType getTypeId() {
return typeId;
}
public void setTypeId(UserType typeId) {
this.typeId = typeId;
}
public boolean isBlocked() {
return isBlocked;
}
public void setBlocked(boolean blocked) {
isBlocked = blocked;
}
}
特权
package entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "privilege")
public class Privilege {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "privilegeId")
private int privilegeId;
@Column(name = "privilegeName", nullable = false)
private String privilegeName;
@ManyToMany(mappedBy = "privilege")
private Set<UserType> usertypes = new HashSet<>();
public Privilege() {}
public Privilege(String privilegeName) {
this.privilegeName = privilegeName;
}
public int getPrivilegeId() {
return privilegeId;
}
public void setPrivilegeId(int privilegeId) {
this.privilegeId = privilegeId;
}
public String getPrivilegeName() {
return privilegeName;
}
public void setPrivilegeName(String privilegeName) { this.privilegeName = privilegeName; }
public Set<UserType> getUsertypes() { return usertypes; }
}
用户类型
package entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "usertype")
public class UserType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "typeId")
private int typeId;
@Column(name = "typeName", nullable = false)
private String typeName;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(
name = "hasprivilege",
joinColumns = { @JoinColumn(name = "typeId") },
inverseJoinColumns = { @JoinColumn(name = "privilegeId") }
)
Set<Privilege> privileges = new HashSet<>();
public UserType() { }
public UserType(String typeName) {
this.typeName = typeName;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
通知
package entity;
import javax.persistence.*;
@Entity
@Table(name = "notification")
public class Notification {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notificationId")
private int notificationId;
@Column(name = "notificationMessage", nullable = false)
private String notificationMessage;
@Column(name = "frequency", nullable = false)
private String frequency;
@Column(name = "create_date", nullable = false)
private String create_date;
@Column(name = "update_date", nullable = false)
private String update_date;
public Notification() {}
public Notification(String notificationMessage, String frequency, String create_date, String update_date) {
this.notificationMessage = notificationMessage;
this.frequency = frequency;
this.create_date = create_date;
this.update_date = update_date;
}
public int getNotificationId() {
return notificationId;
}
public void setNotificationId(int notificationId) {
this.notificationId = notificationId;
}
public String getNotificationMessage() {
return notificationMessage;
}
public void setNotificationMessage(String notificationMessage) {
this.notificationMessage = notificationMessage;
}
public String getFrequency() {
return frequency;
}
public void setFrequency(String frequency) {
this.frequency = frequency;
}
public String getCreate_date() {
return create_date;
}
public void setCreate_date(String create_date) {
this.create_date = create_date;
}
public String getUpdate_date() {
return update_date;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
}
消息
package entity;
import javax.persistence.*;
@Entity
@Table(name = "message")
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "messageId")
private String messageId;
@ManyToOne
@JoinColumn(name = "userId")
private User userId;
@ManyToOne
@JoinColumn(name = "groupId")
private Group groupId;
@Column(name = "message")
private String message;
@Column(name = "create_date")
private String create_date;
@ManyToOne
@JoinColumn(name = "notificationId")
private Notification notificationId;
public Message() { }
public Message(User userId, Group groupId, String message, String create_date, Notification notificationId) {
this.userId = userId;
this.groupId = groupId;
this.message = message;
this.create_date = create_date;
this.notificationId = notificationId;
}
public String getMessageId() {
return messageId;
}
public User getUserId() {
return userId;
}
public Group getGroupId() {
return groupId;
}
public String getMessage() {
return message;
}
public String getCreate_date() {
return create_date;
}
public Notification getNotificationId() {
return notificationId;
}
}
我一直在Stack上寻找类似问题的解决方案,但这些建议对我的情况没有帮助。我现在有点卡住。
任何帮助将不胜感激。谢谢您的宝贵时间。