org.hibernate.MappingException: Could not determine type for: java.util.Set
虽然这个问题问了很多次,我已经使用了所有的建议,但我还是得到了这个错误。
java是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | @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; } } |
而role.java是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | @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; } } |
这是我第一次尝试用JPA进行Hibernate注释。所以任何建议都会很有帮助。
对于休眠,pom.xml的依赖项是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <dependency> <groupId>org.hibernate</groupId> hibernate</artifactId> <version>3.5.4-Final</version> <type>pom</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> hibernate-annotations</artifactId> <version>3.5.4-Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> hibernate-core</artifactId> <version>3.5.4-Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> hibernate-validator</artifactId> <version>3.1.0.GA</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> hibernate-entitymanager</artifactId> <version>3.5.4-Final</version> <type>jar</type> <scope>compile</scope> </dependency> |
我对这个错误一无所知。
谢谢。
我对
解决方案:我在公共getter方法中放置了所有注释。我想,当私有字段和公共getter的注释混合在一个类中时,Hibernate无法处理案例。
在列表字段中添加
1 2 3 |
我猜你在用
解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | @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同上。
就在今天,我发现我无意中遗漏了@jointable注释上方的@manytomany注释。
不说映射是正确的还是错误的,但我认为Hibernate需要一个集合实例,在其中声明字段。
1 2 3 4 | @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER) //@ElementCollection(targetClass=Role.class) @Column(name ="ROLE_ID") private Set<Role> roles = new HashSet<Role>(); |
我也遇到了类似的问题,我发现了一个问题,我将注释混合在属性之上,其中一些在公共方法之上。我只是把它们都放在属性之上,然后它就工作了。
您可能只需要在角色上添加
为什么Java有瞬态字段?
我遇到了一个类似的问题,在这个问题中,我得到了一个类中没有映射到db列的成员的错误,它只是另一个实体列表的持有者。我把列表改为arraylist,错误就消失了。我知道,我真的不应该在一个映射实体中这样做,这就是DTO的目的。只是想分享一下,以防有人发现这个线索,上面的答案不适用或没有帮助。