以下是我的文件。当我尝试在表之间实现一对多和多对一的关系时,我得到一个AnnotationException。我不确定问题是什么,因为我第一次这样做。
这是我的问题模型
@Entity
@Table(name = "ns_question_b")
public class Question extends DomainObject {
@GeneratedValue(strategy = GenerationType.TABLE, generator = "question_gen")
@TableGenerator(name = "question_gen", table = "question_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "QuestionId_Gen", initialValue = 100000, allocationSize = 1000)
@Id
@Column(name = "question_id")
private BigInteger questionId;
@Column(name = "question_text")
private String questionText;
@Column(name = "question_type")
@Enumerated(EnumType.STRING)
private QuestionType questionType;
@Column(name = "question_timeout")
private Double questionTimeout;
@Column(name = "marks")
private Double marks;
@Column(name = "negative_mark")
private Double negativeMark;
@Column(name = "question_position")
private Double questionPosition;
@Column(name = "question_options")
private String questionOptions;
@Column(name = "subject")
private String subject;
@ManyToOne
@JoinColumn(name = "quiz_id",nullable = false)
private Quiz quiz;
public Question() {
super();
}
这是我的测验模型
@Entity
@Table(name = "ns_quiz_b")
public class Quiz extends BaseTest{
@GeneratedValue(strategy = GenerationType.TABLE, generator = "quiz_gen")
@TableGenerator(name = "quiz_gen", table = "quiz_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "QuizId_Gen", initialValue = 100000, allocationSize = 1000)
@Id
@Column(name = "quiz_id")
private BigInteger quizId;
@Column(name = "quiz_title")
private String quizTitle;
@Column(name = "quiz_description")
private String quizDescription;
@OneToMany(mappedBy="quiz")
private List<Question> questionList;
这是我的logcat
Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
Jan 10, 2018 9:09:55 PM org.springframework.boot.SpringApplication reportFailure
SEVERE: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'quizController' defined in file
[/home/kartikeya/Documents/ns-server-apis/target/classes/com/neostencil/modules/testmanagement/controllers/quiz/QuizController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.neostencil.modules.testmanagement.controllers.quiz.QuizController]: Constructor threw exception; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.neostencil.modules.testmanagement.model.entities.quiz.Quiz.questionList[com.neostencil.modules.testmanagement.model.entities.question.Question]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
at com.neostencil.NeoStencilMainApp.main(NeoStencilMainApp.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.neostencil.modules.testmanagement.controllers.quiz.QuizController]: Constructor threw exception; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.neostencil.modules.testmanagement.model.entities.quiz.Quiz.questionList[com.neostencil.modules.testmanagement.model.entities.question.Question]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
... 17 more
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.neostencil.modules.testmanagement.model.entities.quiz.Quiz.questionList[com.neostencil.modules.testmanagement.model.entities.question.Question]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1191)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:794)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:719)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at com.neostencil.modules.testmanagement.model.repositories.QuizRepository.createSessionFactory(QuizRepository.java:57)
at com.neostencil.modules.testmanagement.model.repositories.QuizRepository.<init>(QuizRepository.java:31)
at com.neostencil.modules.testmanagement.services.QuizService.<init>(QuizService.java:23)
at com.neostencil.modules.testmanagement.controllers.quiz.QuizController.<init>(QuizController.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 19 more
这是我的hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:postgresql://xxx
</property>
<property name="connection.username">xxx</property>
<property name="connection.password">xxx</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect
</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">none</property>
<!-- The mapping information of entities -->
<mapping class="com.framework.model.entities.User" />
<mapping class="com.modules.teacher.model.entities.Teacher" />
<mapping class="com.framework.model.entities.common.Duration" />
<mapping class="com.modules.testmanagement.model.entities.quiz.Quiz"/>
<mapping class="com.modules.testmanagement.model.entities.question.Question"/>
</session-factory>
答案 0 :(得分:0)
确保注释为javax.persistence.Entity
,而不是org.hibernate.annotations.Entity
。前者使实体可检测。后者只是一个补充。
答案 1 :(得分:0)
这在Eclipse上对我有用。 如果您使用的是eclipse,只需在生成模型之前选中“使用Java 5语法”。