I am learning Hibernate. However, I have come across this error: <<< FAILURE! org.hibernate.MappingException: Could not determine type for: "some className", at table: "some tableName", for columns: [org.hibernate.mapping.Column(some columnName)].
It seems to be a problem with my annotation mappings I wrote in the data objects. One of them, associates or has properties of the other two, and has two of the same but with different roles as seen here:
@Entity
public class Ranking {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long id;
@Column Person subject; // the person being ranked
@Column Person observer; // the one to give a rank
@Column Skill skill;
@Column Integer ranking;
public Ranking(){};
// --- mutators and accessors ..
}
import javax.persistence.Id;
@Entity // labels this class as a table in the db
public class Person {
@Id // identifies id as PK in the tabale
@GeneratedValue(strategy = GenerationType.AUTO) // autogenerates the id - configurable
private long id;
@Column // labels it as a column to be in the db
String name;
public Person(){};
// ... mutators and accessors
}
The other classes just have simple fields... and they are also mapped using the @Entity and the @Column annotations. However the error seems to indicate that there's a problem with mapping the Person object in the observer column as seen in the error stack below:
[ERROR] Tests run: 7, Failures: 1, Errors: 0, Skipped: 6, Time elapsed: 5.878 s <<< FAILURE! -- in TestSuite
[ERROR] chapter03.hibernate.SkillTest.setup -- Time elapsed: 5.314 s <<< FAILURE!
org.hibernate.MappingException: Could not determine type for: chapter03.simple.Person, at table: Ranking, for columns: [org.hibernate.mapping.Column(observer)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:515)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:482)
at org.hibernate.mapping.Property.isValid(Property.java:231)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:627)
at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:359)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:314)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471)
at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:200)
at chapter03.hibernate.SkillTest.setup(SkillTest.java:28)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:390)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:325)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:382)
at org.testng.SuiteRunner.run(SuiteRunner.java:336)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
at org.testng.TestNG.runSuites(TestNG.java:1114)
at org.testng.TestNG.run(TestNG.java:1082)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:155)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:169)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:88)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:137)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
......
I tried possibly using a single @Column annotation thinking that it could be the problem since they are yet the same class, Person - and I expected that the error would go away and my data would be pushed to the db. But this did not work.
Secondly, I checked with my pom.xml file for the chapter I am in - and it seems fine. I am using the book Begginning Hibernate for Hibernate 5 by Joseph Ottinger, et al - and trying to follow their organisations with some slight changes in the packages. However, with chapter02's trial, it works just fine, and it fails when it comes to chapter03. The code shows no syntax errors at all. The book sample project presents a pom.xml for each chapter and a global one for all chapters. Here is one for chaptero3:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.autumncode.books.hibernate</groupId>
<artifactId>hibernate-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>chapter03</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>[5.0.0,5.9.9]</version>
</dependency>
<!--external connection pool to use instead of the one built in with hibernate.dependency>
Its version should match with the hibernate version.
</dependency>-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
</dependencies>
</project>
I tried checking this, and it looks fine..
Here is the global one.. for now (I only indluded dependencies as I move through the book, but not all of them as is for the final completed book)
<?xml version="1.0"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.autumncode.books.hibernate</groupId>
<artifactId>hibernate-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>chapter01</module>
<module>chapter02</module> <!--a clone of chapter01-->
<module>chapter03</module>
</modules>
<name>hibernate-parent</name>
<!-- <url>http://maven.apache.org</url> -->
<properties>
<project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>[6.9.10,]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>[5.0.0,5.9.9]</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>3.0.0-alpha1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
</project>
The issue you're facing stems from Hibernate's uncertainty about how to map the Person object with the Ranking object.
Both of them represent tables in the database, but you haven't specified the relationship between them. The author of the book you mentioned probably skipped this part to simplify the explanation, intending to introduce it in later chapters
For instance, if you establish a many-to-one relationship in the Ranking class with Person objects, it implies that multiple rankings can be associated with the same Person objects. Essentially, multiple rankings can have one observer.
The type of relationship you choose depends on various factors, and it's your decision to make, whether it's one-to-one, one-to-many, or many-to-many.
To address this in your code, you need to define the relationship explicitly. For example:
Additionally, I found the source code of the author's book on GitHub, which you can refer to: source code