我创建了两个实体类。
RoleEntity:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table (name ="roles")
public class RoleEntity {
@Id
@Column(name = "role_id")
private Integer roleId;
@Column(name = "role_name")
private String roleName;
//Getters
//Setters
UserEntity:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user_master")
public class UserEntity {
@Id
private Integer id;
@Column(name = "user_name")
private String username;
@Column(name = "user_password")
private String password;
//getters
//setters
现在我有一个简单的pojo,它将从这两个实体中获取数据,稍后将在服务中使用。
import java.util.Set;
public class UserRoleAssociationEntity {
UserEntity user;
Set<RoleEntity> roles;
//getters
//setters
现在我在运行项目时遇到错误。
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.dataGuru.BusDirV3.Entities.UserRoleAssociationEntity
如果我使用@entity注释UserRoleAssociationEntity类,则会出现以下错误:
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.dataGuru.BusDirV3.Entities.UserRoleAssociationEntity
我在这里面临的问题是什么?该问题的解决方案。
答案 0 :(得分:1)
您需要在类中具有一个唯一字段,该字段充当此实体的标识符。 (带有if(index == 0)
批注的字段))
答案 1 :(得分:-1)
不是创建新的POJO,而是在UserEntity类中添加了多对多关系。
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user_master")
public class UserEntity
{
@Id
private Integer id;
@Column(name = "user_name")
private String username;
@Column(name = "user_password")
private String password;
@Column(name = "user_password")
private String password;
@ManyToMany(cascade=CascadeType.MERGE, fetch = FetchType.EAGER) //
@JoinTable(
name="USERROLE_ASSOCIATION",
joinColumns={@JoinColumn(name="USER_ID", referencedColumnName="ID")},
inverseJoinColumns={@JoinColumn(name="ROLE_ID", referencedColumnName="ID")})
private Set<RoleEntity> UserRoleAssociationEntity ;