gpt4 book ai didi

java - hibernate不将实体类确定为字段变量

转载 作者:行者123 更新时间:2023-12-01 08:54:48 24 4
gpt4 key购买 nike

我正在尝试为带注释的类创建运行时配置,因为我正在使用其他项目作为库(仅包含 HibernateUtilhibernate.cfg.xml)。现在的问题是,当我在此配置中仅传递一个类时,一切正常,但是当我引入多个类时,测试失败,如下堆栈跟踪:

FAILED CONFIGURATION: @BeforeSuite setUp
org.hibernate.MappingException: Could not determine type for: com.model.Person, at table: ranking, for columns: [org.hibernate.mapping.Column(ranking_observer)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:455)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422)
at org.hibernate.mapping.Property.isValid(Property.java:226)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597)
at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at com.utlity.HibernateUtil.configurationSetup(HibernateUtil.java:15)
at com.model.RankingTest.setUp(RankingTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:216)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:143)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
at org.testng.TestNG.runSuites(TestNG.java:1104)
at org.testng.TestNG.run(TestNG.java:1076)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

以下是我的主课

@Entity
@Table(name = "ranking")
public class Ranking {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ranking_id")
protected Long id;

@Column(name = "ranking_subject")
protected Person subject;

@Column(name = "ranking_observer")
protected Person observer;

@Column(name = "ranking_skill")
protected Skill skill;

@Column(name = "ranking_numbers")
protected Integer ranking;

测试用例

public class RankingTest {

protected SessionFactory factory;

@BeforeSuite
public void setUp() {
factory = HibernateUtil.configurationSetup(Ranking.class, Person.class, Skill.class);
}

@Test
public void testSetup() {
Assert.assertNotNull(factory);
}
}

和 hibernateUtil

public static SessionFactory configurationSetup(Class<?>... classes) {
Configuration configuration = new Configuration();

for (Class<?> clazz : classes) {
configuration.addAnnotatedClass(clazz);
}
configuration.configure("hibernate.cfg.xml");
return configuration.buildSessionFactory();
}

感谢任何帮助,如果需要更多信息,请告诉我。

谢谢

最佳答案

您想要在两种类型的对象之间创建关系:RankingPerson。 @Column 注释通常不用于这些情况,据我所知,使用它会创建一个将 Person 实例序列化到其中的 blob 列。

您需要使用 @ManyToOne 注释(它是 Person 类中的 @OneToMany 的逆向注释。有关更多信息实体关系可以在 the Hibernate documentation 中找到。

关于java - hibernate不将实体类确定为字段变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42107501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com