Skip to content

Commit 69132ac

Browse files
authored
BAEL-8210: Fixing Hibernate AnnotationException: Field is a @manytoone association and May Not Use @column (#18665)
1 parent 98bef4e commit 69132ac

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.hibernate.annotationexception;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.hibernate.SessionFactory;
7+
import org.hibernate.boot.Metadata;
8+
import org.hibernate.boot.MetadataSources;
9+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
10+
import org.hibernate.service.ServiceRegistry;
11+
12+
public class HibernateUtil {
13+
private static SessionFactory sessionFactory;
14+
15+
public static SessionFactory getSessionFactory() {
16+
if (sessionFactory == null) {
17+
Map<String, Object> settings = new HashMap<>();
18+
settings.put("hibernate.connection.driver_class", "org.h2.Driver");
19+
settings.put("hibernate.connection.url", "jdbc:h2:mem:test");
20+
settings.put("hibernate.connection.username", "sa");
21+
settings.put("hibernate.connection.password", "");
22+
settings.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
23+
settings.put("hibernate.show_sql", "true");
24+
settings.put("hibernate.hbm2ddl.auto", "update");
25+
26+
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().applySettings(settings)
27+
.build();
28+
29+
Metadata metadata = new MetadataSources(standardRegistry).addAnnotatedClasses(Student.class, University.class)
30+
.getMetadataBuilder()
31+
.build();
32+
33+
sessionFactory = metadata.getSessionFactoryBuilder()
34+
.build();
35+
}
36+
37+
return sessionFactory;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.hibernate.annotationexception;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.JoinColumn;
7+
import jakarta.persistence.ManyToOne;
8+
9+
@Entity
10+
public class Student {
11+
@Id
12+
private int id;
13+
14+
@Column(name = "full_name")
15+
private String fullName;
16+
17+
@ManyToOne
18+
// switch these two lines to reproduce the exception
19+
// @Column(name = "university")
20+
@JoinColumn(name = "university_id")
21+
private University university;
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int id) {
28+
this.id = id;
29+
}
30+
31+
public String getFullName() {
32+
return fullName;
33+
}
34+
35+
public void setFullName(String fullName) {
36+
this.fullName = fullName;
37+
}
38+
39+
public University getUniversity() {
40+
return university;
41+
}
42+
43+
public void setUniversity(University university) {
44+
this.university = university;
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.hibernate.annotationexception;
2+
3+
import jakarta.persistence.Column;
4+
import jakarta.persistence.Entity;
5+
import jakarta.persistence.Id;
6+
7+
@Entity
8+
public class University {
9+
@Id
10+
private int id;
11+
12+
@Column(name = "university_name")
13+
private String name;
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.hibernate.annotationexception;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.hibernate.Session;
6+
import org.hibernate.query.Query;
7+
import org.junit.jupiter.api.Test;
8+
9+
class ColumnNotAllowedOnManyToOneUnitTest {
10+
11+
/*
12+
* uncomment this test case to test the exception
13+
@Test
14+
void whenUsingColumnAnnotationWithManyToOneAnnotation_thenThrowAnnotationException() {
15+
assertThatThrownBy(() -> {
16+
HibernateUtil.getSessionFactory()
17+
.openSession();
18+
}).isInstanceOf(AnnotationException.class)
19+
.hasMessageContaining("university' is a '@ManyToOne' association and may not use '@Column'");
20+
}
21+
*/
22+
23+
@Test
24+
void whenNotUsingColumnAnnotationWithManyToOneAnnotation_thenCorrect() {
25+
Session session = HibernateUtil.getSessionFactory()
26+
.openSession();
27+
28+
Query<Student> query = session.createQuery("FROM Student", Student.class);
29+
30+
assertThat(query.list()).isEmpty();
31+
32+
session.close();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)