org.hibernate。MappingException:无法为:java.util确定类型。设置(复制)

时间:2022-01-28 19:44:22

This question already has an answer here:

这个问题已经有了答案:

Although this question asked many times and I have already used all the suggestion but still I am getting this error.

虽然这个问题问了很多次,我已经使用了所有的建议,但我还是会犯这个错误。

The User.java is

用户。java是

@Entity
@Table(name = "USER")
public class User implements UserDetails, Serializable {

    private static final long serialVersionUID = 2L;

    @Id
    @Column(name = "USER_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "USERNAME")
    private String username;
    @Column(name = "PASSWORD")
    private String password;
    @Column(name = "NAME")
    private String name;
    @Column(name = "EMAIL")
    private String email;
    @Column(name = "LOCKED")
    private boolean locked;
    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @ElementCollection(targetClass=Role.class)
    @Column(name = "ROLE_ID")
    private Set<Role> roles;

    @Override
    public GrantedAuthority[] getAuthorities() {
        List<GrantedAuthorityImpl> list = new ArrayList<GrantedAuthorityImpl>(0);
        for (Role role : roles) {
            list.add(new GrantedAuthorityImpl(role.getRole()));
        }
        return (GrantedAuthority[]) list.toArray(new GrantedAuthority[list.size()]);
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !isLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public Set<Role> getRoles() {
        return roles;
    }
}

And the Role.java is

和作用。java是

@Entity
@Table(name="ROLE")
public class Role implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ROLE_ID")
    private long id;
    @Column(name="USERNAME")
    private String username;
    @Column(name="ROLE")
    private String role;


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

This is my first attempt in hibernate annotation with JPA. So any suggestions will be very helpful.

这是我第一次尝试使用JPA使用hibernate注释。所以任何建议都是很有帮助的。

For hibernate the pom.xml's dependencies are:

hibernate砰的一声。xml的依赖关系:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.5.4-Final</version>
        <type>pom</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.4-Final</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.5.4-Final</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>3.1.0.GA</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.5.4-Final</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

I have no clue about the fault.

我对这错误一无所知。

Thanks.

谢谢。

9 个解决方案

#1


65  

I got the same problem with @ManyToOne column. It was solved... in stupid way. I had all other annotations for public getter methods, because they were overridden from parent class. But last field was annotated for private variable like in all other classes in my project. So I got the same MappingException without the reason.

@ManyToOne栏目也有同样的问题。它解决了…以愚蠢的方式。我还有其他所有公共getter方法的注释,因为它们是从父类中重写的。但是最后一个字段被注释为private变量,就像我项目中的其他类一样。所以我得到了同样的MappingException。

Solution: I placed all annotations at public getter methods. I suppose, Hibernate can't handle cases, when annotations for private fields and public getters are mixed in one class.

解决方案:我将所有注释放在public getter方法中。我认为,当私有字段和公共getter的注释混合在一个类中时,Hibernate不能处理这些情况。

#2


39  

Adding the @ElementCollection to the List field solved this issue:

将@ElementCollection添加到List字段解决了这个问题:

@Column
@ElementCollection(targetClass=Integer.class)
private List<Integer> countries;

#3


15  

My guess is you are using a Set<Role> in the User class annotated with @OneToMany. Which means one User has many Roles. But on the same field you use the @Column annotation which makes no sense. One-to-many relationships are managed using a separate join table or a join column on the many side, which in this case would be the Role class. Using @JoinColumn instead of @Column would probably fix the issue, but it seems semantically wrong. I guess the relationship between role and user should be many-to-many.

我的猜测是您正在使用带有@OneToMany注释的用户类中的Set 。这意味着一个用户有许多角色。但是在同一个字段中,使用@Column注释是没有意义的。一对多关系是使用一个单独的联接表或多个方面的联接列来管理的,在本例中,这将是角色类。使用@JoinColumn代替@Column可能会解决这个问题,但这似乎是错误的。我猜角色和用户之间的关系应该是多对多。

#4


4  

Not saying your mapping is correct or wrong but I think hibernate wants a instance of the set where you declare the field.

并不是说映射是正确的或错误的,但是我认为hibernate需要在声明字段的地方设置一个实例。

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
//@ElementCollection(targetClass=Role.class)
@Column(name = "ROLE_ID")
private Set<Role> roles = new HashSet<Role>();

#5


3  

Had this issue just today and discovered that I inadvertently left off the @ManyToMany annotation above the @JoinTable annotation.

今天刚刚出了这个问题,我无意中漏掉了@ManyToMany注释,在@ joint - able注释之上。

#6


1  

Solution:

解决方案:

@Entity
@Table(name = "USER")
@Access(AccessType.FIELD)
public class User implements UserDetails, Serializable {

    private static final long serialVersionUID = 2L;

    @Id
    @Column(name = "USER_ID", updatable=false, nullable=false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "USERNAME")
    private String username;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "NAME")
    private String name;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "LOCKED")
    private boolean locked;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Role.class)
    @JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
    private Set<Role> roles;

    @Override
    public GrantedAuthority[] getAuthorities() {
        List<GrantedAuthorityImpl> list = new ArrayList<GrantedAuthorityImpl>(0);
        for (Role role : roles) {
            list.add(new GrantedAuthorityImpl(role.getRole()));
        }
        return (GrantedAuthority[]) list.toArray(new GrantedAuthority[list.size()]);
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !isLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    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 getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}

Role.java same as above.

的角色。java同上。

#7


1  

You may just need to add @Transient annotations on roles to not serialize the set.

您可能只需要在角色上添加@ transitionannotations就可以不序列化集合。

Why does Java have transient fields?

为什么Java有瞬变字段?

#8


1  

I had similar problem I found the issue I was mixing the annotations some of them above the attributes and some of them above public methods. I just put all of them above attributes and it works.

我遇到了类似的问题,我发现问题是我把注释混合在属性和公共方法之上。我把它们都放在属性的上面,它就可以工作了。

#9


0  

I had a similar issue where I was getting an error for a member in the class that wasn't mapped to the db column, it was just a holder for a List of another entity. I changed List to ArrayList and the error went away. I know, I really shouldn't do that in a mapped entity, and that's what DTO's are for. Just wanted to share in case someone finds this thread and the answers above don't apply or help.

我也遇到过类似的问题,我在类中得到一个未映射到db列的成员的错误,它只是另一个实体的列表的持有者。我将List改为ArrayList,错误消失了。我知道,我真的不应该在一个映射的实体中这样做,这就是DTO的目的。只是想分享,以防有人发现这条线,上面的答案不适用或帮助。

#1


65  

I got the same problem with @ManyToOne column. It was solved... in stupid way. I had all other annotations for public getter methods, because they were overridden from parent class. But last field was annotated for private variable like in all other classes in my project. So I got the same MappingException without the reason.

@ManyToOne栏目也有同样的问题。它解决了…以愚蠢的方式。我还有其他所有公共getter方法的注释,因为它们是从父类中重写的。但是最后一个字段被注释为private变量,就像我项目中的其他类一样。所以我得到了同样的MappingException。

Solution: I placed all annotations at public getter methods. I suppose, Hibernate can't handle cases, when annotations for private fields and public getters are mixed in one class.

解决方案:我将所有注释放在public getter方法中。我认为,当私有字段和公共getter的注释混合在一个类中时,Hibernate不能处理这些情况。

#2


39  

Adding the @ElementCollection to the List field solved this issue:

将@ElementCollection添加到List字段解决了这个问题:

@Column
@ElementCollection(targetClass=Integer.class)
private List<Integer> countries;

#3


15  

My guess is you are using a Set<Role> in the User class annotated with @OneToMany. Which means one User has many Roles. But on the same field you use the @Column annotation which makes no sense. One-to-many relationships are managed using a separate join table or a join column on the many side, which in this case would be the Role class. Using @JoinColumn instead of @Column would probably fix the issue, but it seems semantically wrong. I guess the relationship between role and user should be many-to-many.

我的猜测是您正在使用带有@OneToMany注释的用户类中的Set 。这意味着一个用户有许多角色。但是在同一个字段中,使用@Column注释是没有意义的。一对多关系是使用一个单独的联接表或多个方面的联接列来管理的,在本例中,这将是角色类。使用@JoinColumn代替@Column可能会解决这个问题,但这似乎是错误的。我猜角色和用户之间的关系应该是多对多。

#4


4  

Not saying your mapping is correct or wrong but I think hibernate wants a instance of the set where you declare the field.

并不是说映射是正确的或错误的,但是我认为hibernate需要在声明字段的地方设置一个实例。

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
//@ElementCollection(targetClass=Role.class)
@Column(name = "ROLE_ID")
private Set<Role> roles = new HashSet<Role>();

#5


3  

Had this issue just today and discovered that I inadvertently left off the @ManyToMany annotation above the @JoinTable annotation.

今天刚刚出了这个问题,我无意中漏掉了@ManyToMany注释,在@ joint - able注释之上。

#6


1  

Solution:

解决方案:

@Entity
@Table(name = "USER")
@Access(AccessType.FIELD)
public class User implements UserDetails, Serializable {

    private static final long serialVersionUID = 2L;

    @Id
    @Column(name = "USER_ID", updatable=false, nullable=false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "USERNAME")
    private String username;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "NAME")
    private String name;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "LOCKED")
    private boolean locked;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Role.class)
    @JoinTable(name = "USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
    private Set<Role> roles;

    @Override
    public GrantedAuthority[] getAuthorities() {
        List<GrantedAuthorityImpl> list = new ArrayList<GrantedAuthorityImpl>(0);
        for (Role role : roles) {
            list.add(new GrantedAuthorityImpl(role.getRole()));
        }
        return (GrantedAuthority[]) list.toArray(new GrantedAuthority[list.size()]);
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !isLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    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 getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}

Role.java same as above.

的角色。java同上。

#7


1  

You may just need to add @Transient annotations on roles to not serialize the set.

您可能只需要在角色上添加@ transitionannotations就可以不序列化集合。

Why does Java have transient fields?

为什么Java有瞬变字段?

#8


1  

I had similar problem I found the issue I was mixing the annotations some of them above the attributes and some of them above public methods. I just put all of them above attributes and it works.

我遇到了类似的问题,我发现问题是我把注释混合在属性和公共方法之上。我把它们都放在属性的上面,它就可以工作了。

#9


0  

I had a similar issue where I was getting an error for a member in the class that wasn't mapped to the db column, it was just a holder for a List of another entity. I changed List to ArrayList and the error went away. I know, I really shouldn't do that in a mapped entity, and that's what DTO's are for. Just wanted to share in case someone finds this thread and the answers above don't apply or help.

我也遇到过类似的问题,我在类中得到一个未映射到db列的成员的错误,它只是另一个实体的列表的持有者。我将List改为ArrayList,错误消失了。我知道,我真的不应该在一个映射的实体中这样做,这就是DTO的目的。只是想分享,以防有人发现这条线,上面的答案不适用或帮助。