简体   繁体   中英

Caused by: java.lang.IllegalArgumentException: Not a managed type: & Caused by: org.hibernate.AnnotationException: No identifier specified for entity:

I have created two entity classes.

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

Now i have a simple pojo which will take data from these two entities and later will be used in a service.

import java.util.Set;

public class UserRoleAssociationEntity {

UserEntity user;
Set<RoleEntity> roles;
//getters
//setters

Now I am getting error when I run the project.

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.dataGuru.BusDirV3.Entities.UserRoleAssociationEntity

If I annotate the UserRoleAssociationEntity class with @entity i get the following error:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.dataGuru.BusDirV3.Entities.UserRoleAssociationEntity

What is the problem which i am facing here & solution for this problem.

You need to have a unique field in your class which acts as an identifier for this entity. (Field with @Id annotation`)

Instead of creating a new POJO, add a many-to-many relation ship in the UserEntity Class like.

 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 ;

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