OneToMany & ManyToOne mapping Spring Boot / Hibernate
我有两张有外交关系的桌子。我尝试过寻找如何做到这一点,它总是会导致一个Tomany&Manytoone映射。我有这两张桌子。
用户角色
。
用户
氧化镁
我试图通过将"职位"列的值获取到"用户角色"表来显示所有用户并显示他们的职位。
这些是我的课
用户.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 | @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; .......... private UserRole userRole; @ManyToOne() @JoinColumn(name="role_id") public UserRole getUserRole() { return userRole; } public void setUserRole(UserRole userRole) { this.userRole = userRole; } ....... } |
用户角色.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 | @Entity @Table(name="user_role") public class UserRole { ....... private List<User> user; @OneToMany(targetEntity=User.class, mappedBy="userRole",cascade=CascadeType.ALL, fetch = FetchType.LAZY) public List<User> getUser() { return user; } public void setUser(List<User> user) { this.user = user; } public long getRole_id() { return role_id; } public void setRole_id(long role_id) { this.role_id = role_id; } ......... } |
号
我不断地得到这个错误。
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 | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user_role, for columns: [org.hibernate.mapping.Column(user)] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] at com.rtc_insurance.AdminApplication.main(AdminApplication.java:10) [classes/:na] Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user_role, for columns: [org.hibernate.mapping.Column(user)] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:402) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] ... 16 common frames omitted Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user_role, for columns: [org.hibernate.mapping.Column(user)] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:456) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:423) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.mapping.Property.isValid(Property.java:226) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.0.9.RELEASE.jar:5.0.9.RELEASE] ... 20 common frames omitted |
这是我第一次使用Spring,所以我不太熟悉。任何帮助都将不胜感激。
你能把关系移到这样的相关对象上吗?
1 2 3 | @ManyToOne() @JoinColumn(name="role_id", referencedColumnName ="role_id", insertable = false, updatable = false) private UserRole userRole; |
用户角色也一样
1 2 | @OneToMany(targetEntity=User.class, mappedBy="userRole",cascade=CascadeType.ALL, fetch = FetchType.LAZY) private List<User> user = new ArrayList<>(); |
。
更新
JAR文件似乎已损坏。尝试从.m2
epository和EDOCX1[3]中删除内容
或
右键单击项目,选择Maven,更新项目,检查快照/版本的强制更新。
我可以通过混合字段和属性之间的注释映射来重新创建您的异常。我在上面描述的问题中看到,您注释字段ID,然后注释属性userrole,如果我这样做,就会得到相同的错误。我可以通过只注释属性或字段来修复错误。这两种模式都可以工作:
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 | @Entity @Table(name ="USER") public class User { private Long id; private UserRole userRole; String userName; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name ="USER_NAME") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @ManyToOne() @JoinColumn(name="role_id") public UserRole getUserRole() { return userRole; } public void setUserRole(UserRole userRole) { this.userRole = userRole; } } |
这也适用于:
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 | @Entity @Table(name ="USER") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne() @JoinColumn(name="role_id") private UserRole userRole; @Column(name ="USER_NAME") String userName; 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 UserRole getUserRole() { return userRole; } public void setUserRole(UserRole userRole) { this.userRole = userRole; } } |
。
注释字段的其他模型对象的示例
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 | @Entity @Table(name ="USER") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne() @JoinColumn(name="role_id") private UserRole userRole; @Column(name ="USER_NAME") String userName; 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 UserRole getUserRole() { return userRole; } public void setUserRole(UserRole userRole) { this.userRole = userRole; } } |
。
测试代码(我添加了这个代码,因为经常会混淆必须设置双向关系才能让用户和用户角色在保存到用户角色时保持不变)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | List<User> users = new ArrayList<>(); User user1 = new User(); user1.setUserName("user1"); users.add(user1); User user2 = new User(); user2.setUserName("user2"); users.add(user2); UserRole userRole = new UserRole(); userRole.setRoleName("admin"); //Unidirectional relationship user1.setUserRole(userRole); user2.setUserRole(userRole); //set Bidirectional relationship userRole.setUsers(users); userRole = userRoleRepository.save(userRole); //Show that the two users and the UserRole persisted UserRole result = userRoleRepository.findById(userRole.getId()).get(); assertEquals(2, result.getUsers().size()); |
只是一个提示,检查您的实体类,并确保在类声明语句之前包含@entity annotation。
1 2 3 4 | @Entity public class MyEntityClass{ //code } |
。
然后,在使用上述myEntityClass的任何其他实体类上,@manytomany或@onetomany关系下添加以下内容。
1 2 3 | @OneToMany(mappedBy="mappingItem", cascade=CascadeType.ALL, orphanRemoval=true) @JsonIgnore private List<MyEntityClass> myEntityClassItemList; |
这种方法对我有一定的帮助。
你必须注解Java字段,而不是GETTER。
1 2 | @OneToMany(targetEntity=User.class, mappedBy="userRole",cascade=CascadeType.ALL, fetch = FetchType.LAZY) private List<User> user; |
对于
1 2 3 | @ManyToOne @JoinColumn(name="role_id") private UserRole userRole; |
号
另外,您是否在