Reputation: 99
I am trying to create runtime configuration for annotated classes as I am using other project as library (contains only HibernateUtil
and hibernate.cfg.xml
). Now the problem is that when I pass only one class in this configuration, everything works fine but when I introduce more then one classes, test fails as following stack trace:
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)
Following is my main class
@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;
test case
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);
}
}
and 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();
}
Any help is appreciated and if more info is needed, please tell me.
Thanks
Upvotes: 0
Views: 102
Reputation: 519
You want to create a relationship between two types of objects: Ranking
and Person
. The @Column
-annotation is not usually used for those situations, AFAIK using it results in creating a blob column into which the Person instance is serialized.
You'd want to use the @ManyToOne
annotation instead (and it's inverse @OneToMany
in the Person
-class. More information about entity relationships can be found in the Hibernate documentation.
Upvotes: 1