JPA Entity with two fields of the same type
我试图在我的实体中有两个相同域类的字段,但出现了以下错误:
org.hibernate.MappingException: Could not determine type for: com.packt.webapp.domain.User, at table: opinions, for columns: [org.hibernate.mapping.Column(author)]
号
我的实体:
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 | @Entity @Table(name="opinions") public class Opinion { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @NotNull private String text; @NotNull private String date; @ManyToOne @JoinColumn(name="commented_user") private User writtenTo; private User author; @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String username; private String password; @OneToMany(mappedBy="writtenTo") private List<Opinion> opinions; |
我只想将意见映射到
作者也尝试诠释??????? </P >
1 2 3 | @ManyToOne @JoinColumn(name="author") private User author; |
它的complaining说它不知道如何的LSP的
如果你很喜欢的一场IGNORE映射方法,它与
实体的意见: </P >
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 | @Entity @Table(name="opinions") public class Opinion { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @NotNull private String text; @NotNull private String date; @ManyToOne @JoinColumn(name="commented_user") private User writtenTo; // map author to User entity @ManyToOne @JoinColumn(name="authored_user") private User author; // getters and setters } |
用户实体: </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; private String username; private String password; @OneToMany(mappedBy="writtenTo") private List<Opinion> opinions; // map opinions to the author @OneToMany(mappedBy="author") private List<Opinion> authoredOpinions; // getters and setters } |
是的manytoone apply"的诠释都在"用户域。 </P >
1 2 3 4 5 6 | @ManyToOne @JoinColumn(name="commented_user") private User writtenTo; @ManyToOne @JoinColumn(name="author") private User author; |
但是,有一个更灵活的解决方案,这样的问题。replace"和"多点之间的关系,通过"manytoone manytomany酮类化合物。创建一个用户和一个角色(实体与descendats中心承担与特异性的字段)。一种用户可以有很多的角色(作者、作者等)和一个角色可能played由许多用户。在这个案例的你不能改变你的想法和remove attach create / / /分离的角色dynamically没有任何数据结构的变化的是现有的表。 </P >
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 | @Entity public class User { @Id private Long id; @Column private String name; @ManyToMany @JoinTable( name="User_Role", joinColumns=@JoinColumn(name="UserID", referencedColumnName="ID"), inverseJoinColumns=@JoinColumn(name="RoleID", referencedColumnName="ID")) private List<Role> roles; } @Entity public class Role { @Id private Long id; @Column private String name; @ManyToMany( mappedBy="roles" ) private List<User> users; } |
和你可以得到/检查用户的角色,通过角色的IDS /名称使用A型多功能类: </P >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | puclic class RoleUtility { public Role getUserRoleByName( User user_, String roleName_ ) { User retRole = null; Iterator<Role> i = roles_.iterator(); while ( ( retRole == null ) && i.hasNext() ) { Role role = (Role) i.next(); if ( roleName_.isEqual( role.getName ) ) retRole = role; } return retRole; } } |
在客户端代码的检查:A的作用 </P >
1 2 | User user = ... Role role = RoleUtility.getRoleByName( user.getRoles(), roleName ); |
呆在你以与这个解决方案,你可以添加一个御史/主持人的意见或某类,没有任何数据结构的变化。 </P >