diff --git a/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/HibernateUtil.java b/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/HibernateUtil.java new file mode 100644 index 000000000000..0f9865840733 --- /dev/null +++ b/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/HibernateUtil.java @@ -0,0 +1,39 @@ +package com.baeldung.hibernate.annotationexception; + +import java.util.HashMap; +import java.util.Map; + +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; + +public class HibernateUtil { + private static SessionFactory sessionFactory; + + public static SessionFactory getSessionFactory() { + if (sessionFactory == null) { + Map settings = new HashMap<>(); + settings.put("hibernate.connection.driver_class", "org.h2.Driver"); + settings.put("hibernate.connection.url", "jdbc:h2:mem:test"); + settings.put("hibernate.connection.username", "sa"); + settings.put("hibernate.connection.password", ""); + settings.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); + settings.put("hibernate.show_sql", "true"); + settings.put("hibernate.hbm2ddl.auto", "update"); + + ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().applySettings(settings) + .build(); + + Metadata metadata = new MetadataSources(standardRegistry).addAnnotatedClasses(Student.class, University.class) + .getMetadataBuilder() + .build(); + + sessionFactory = metadata.getSessionFactoryBuilder() + .build(); + } + + return sessionFactory; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/Student.java b/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/Student.java new file mode 100644 index 000000000000..3d6ac9ce06d4 --- /dev/null +++ b/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/Student.java @@ -0,0 +1,47 @@ +package com.baeldung.hibernate.annotationexception; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; + +@Entity +public class Student { + @Id + private int id; + + @Column(name = "full_name") + private String fullName; + + @ManyToOne + // switch these two lines to reproduce the exception + // @Column(name = "university") + @JoinColumn(name = "university_id") + private University university; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public University getUniversity() { + return university; + } + + public void setUniversity(University university) { + this.university = university; + } + +} diff --git a/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/University.java b/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/University.java new file mode 100644 index 000000000000..a8bec4feffbe --- /dev/null +++ b/persistence-modules/hibernate-exceptions-2/src/main/java/com/baeldung/hibernate/annotationexception/University.java @@ -0,0 +1,31 @@ +package com.baeldung.hibernate.annotationexception; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class University { + @Id + private int id; + + @Column(name = "university_name") + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/persistence-modules/hibernate-exceptions-2/src/test/java/com/baeldung/hibernate/annotationexception/ColumnNotAllowedOnManyToOneUnitTest.java b/persistence-modules/hibernate-exceptions-2/src/test/java/com/baeldung/hibernate/annotationexception/ColumnNotAllowedOnManyToOneUnitTest.java new file mode 100644 index 000000000000..e5d718d7cd65 --- /dev/null +++ b/persistence-modules/hibernate-exceptions-2/src/test/java/com/baeldung/hibernate/annotationexception/ColumnNotAllowedOnManyToOneUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.hibernate.annotationexception; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.hibernate.Session; +import org.hibernate.query.Query; +import org.junit.jupiter.api.Test; + +class ColumnNotAllowedOnManyToOneUnitTest { + + /* + * uncomment this test case to test the exception + @Test + void whenUsingColumnAnnotationWithManyToOneAnnotation_thenThrowAnnotationException() { + assertThatThrownBy(() -> { + HibernateUtil.getSessionFactory() + .openSession(); + }).isInstanceOf(AnnotationException.class) + .hasMessageContaining("university' is a '@ManyToOne' association and may not use '@Column'"); + } + */ + + @Test + void whenNotUsingColumnAnnotationWithManyToOneAnnotation_thenCorrect() { + Session session = HibernateUtil.getSessionFactory() + .openSession(); + + Query query = session.createQuery("FROM Student", Student.class); + + assertThat(query.list()).isEmpty(); + + session.close(); + } + +}