设计多对多关系时抛出org.springframework.beans.factory.BeanCreationException异常。

huangapple 未分类评论49阅读模式
英文:

design many-to-many relationship throwing org.springframework.beans.factory.BeanCreationException

问题

I want to design a many-to-many relationship for students to choose courses. At first, I used the many-to-many relationship directly, and then reported an error. Because I am a novice, I am not very clear about the specific rules. I suspect that there cannot be two relationships between the two entities, so I changed to the following way of writing, but it still reported the same error. I think I did follow the tutorial, but I really don't know where is wrong. So I have to ask for help.

User

@Entity
@Table(name = "user")
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})
public class User {
    // ... (other attributes and getters/setters)

    @OneToMany(targetEntity = UserCourse.class, mappedBy = "student")
    private List<UserCourse> ars;
    // ... (getter and setter for 'ars')
}

Course

@Entity
@Table(name = "course")
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})
public class Course {
    // ... (other attributes and getters/setters)

    @OneToMany(targetEntity = UserCourse.class, mappedBy = "course")
    private List<UserCourse> ars;
    // ... (getter and setter for 'ars')
}

UserCourse

@Entity
@Table(name = "user_course")
@JsonIgnoreProperties({"handler", "hibernateLazyInitializer"})
public class UserCourse {
    // ... (other attributes and getters/setters)

    @ManyToOne
    @JoinColumn(name = "uid")
    private User student;
    // ... (getter and setter for 'student')

    @ManyToOne
    @JoinColumn(name = "cid")
    private Course course;
    // ... (getter and setter for 'course')
}

Error Message:

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, for columns: [org.hibernate.mapping.Column(ars)]
    ...
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(ars)]
    ...

Thank you very much for your help.

英文:

I want to design a many-to-many relationship for students to choose courses. At first, I used the many-to-many relationship directly, and then reported an error. Because I am a novice, I am not very clear about the specific rules. I suspect that there cannot be two relationships between the two entities, so I changed to the following way of writing, but it still reported the same error. I think I did follow the tutorial, but I really dont know where is wrong. So I have to ask for help.Below is the code.

User

@Entity
@Table(name = &quot;user&quot;)
@JsonIgnoreProperties({&quot;handler&quot;, &quot;hibernateLazyInitializer&quot;})
public class User {
    private int id;
    private int rid;
    private String name;
    private String password;
    private String salt;
    private String email;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = &quot;email&quot;)
    public String getEmail(){
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(name = &quot;name&quot;)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = &quot;password&quot;)
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Basic
    @Column(name = &quot;salt&quot;)
    public String getSalt() {
        return salt;
    }
    public void setSalt(String salt) {
        this.salt = salt;
    }

    @Basic
    @Column(name = &quot;rid&quot;)
    public int getRid() {
        return rid;
    }
    public void setRid(int rid) {
        this.rid = rid;
    }

    @OneToMany(targetEntity=UserCourse.class,mappedBy = &quot;student&quot;)
    private List&lt;UserCourse&gt; ars;
    public List&lt;UserCourse&gt; getArs() {
        return ars;
    }
    public void setArs(List&lt;UserCourse&gt; ars) {
        this.ars = ars;
    }
}

Course

@Entity
@Table(name = &quot;course&quot;)
@JsonIgnoreProperties({ &quot;handler&quot;,&quot;hibernateLazyInitializer&quot; })
public class Course {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Column(name = &quot;name&quot;)
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name=&quot;sid&quot;)
    private Subject subject;
    public Subject getSubject() {
        return subject;
    }
    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    @ManyToOne
    @JoinColumn(name=&quot;tid&quot;)
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

    @OneToMany(targetEntity=UserCourse.class,mappedBy = &quot;course&quot;)
    private List&lt;UserCourse&gt; ars;
    public List&lt;UserCourse&gt; getArs() {
        return ars;
    }
    public void setArs(List&lt;UserCourse&gt; ars) {
        this.ars = ars;
    }
}

UserCourse

@Entity
@Table(name = &quot;user_course&quot;)
@JsonIgnoreProperties({ &quot;handler&quot;,&quot;hibernateLazyInitializer&quot; })
public class UserCourse {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    private int id;
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }


    @ManyToOne
    @JoinColumn(name = &quot;uid&quot;)
    private User student;
    public User getStudent() {
        return student;
    }
    public void setStudent(User student) {
        this.student = student;
    }

    @ManyToOne
    @JoinColumn(name = &quot;cid&quot;)
    private Course course;
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }
}

and the error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;entityManagerFactory&#39; 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, for columns: [org.hibernate.mapping.Column(ars)]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at com.tz.mooc.Application.main(Application.java:10) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]
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, for columns: [org.hibernate.mapping.Column(ars)]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 21 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: user, for columns: [org.hibernate.mapping.Column(ars)]
	at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:488) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:455) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.mapping.Property.isValid(Property.java:227) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:343) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237) ~[hibernate-core-5.4.9.Final.jar:5.4.9.Final]
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 25 common frames omitted

Thank you very much for your help

答案1

得分: 0

最后,我只是自己在数据库中创建了一张表,并将用户-课程表视为普通表,以避免使用"@ManyToMany"。我认为这不是最佳方法,看起来很混乱。也许有人会给我更好的答案。我认为最好创建一个演示来测试。

英文:

finally I just create a table in db by myself, and treat the user-course table like normal to avoid "@ManyToMany". I think it is not a best way, and it looks confused. Maybe someone will give me the better answer. I think it is better to create a demo to test.

huangapple
  • 本文由 发表于 2020年4月9日 19:05:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61119701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定