diff --git a/spring-exceptions/src/main/java/org/baeldung/ex/mappingexception/cause4/persistence/model/Foo.java b/spring-exceptions/src/main/java/org/baeldung/ex/mappingexception/cause4/persistence/model/Foo.java new file mode 100644 index 0000000000..11e71edf01 --- /dev/null +++ b/spring-exceptions/src/main/java/org/baeldung/ex/mappingexception/cause4/persistence/model/Foo.java @@ -0,0 +1,31 @@ +package org.baeldung.ex.mappingexception.cause4.persistence.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + public Foo() { + super(); + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + +} diff --git a/spring-exceptions/src/main/resources/hibernate-mysql.properties b/spring-exceptions/src/main/resources/hibernate-mysql.properties new file mode 100644 index 0000000000..5e8c7be141 --- /dev/null +++ b/spring-exceptions/src/main/resources/hibernate-mysql.properties @@ -0,0 +1,8 @@ +hibernate.connection.username=tutorialuser +hibernate.connection.password=tutorialmy5ql +hibernate.connection.driver_class=com.mysql.jdbc.Driver +hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +#hibernate.connection.datasource=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true +hibernate.connection.url=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true +hibernate.show_sql=false +hibernate.hbm2ddl.auto=create diff --git a/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java b/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java new file mode 100644 index 0000000000..37e624b16c --- /dev/null +++ b/spring-exceptions/src/test/java/org/baeldung/ex/mappingexception/Cause4MappingExceptionIntegrationTest.java @@ -0,0 +1,39 @@ +package org.baeldung.ex.mappingexception; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.baeldung.ex.mappingexception.cause4.persistence.model.Foo; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.service.ServiceRegistryBuilder; +import org.junit.Test; + +public class Cause4MappingExceptionIntegrationTest { + + // tests + + @Test + public final void givenEntityIsPersisted_thenException() throws IOException { + final Configuration configuration = new Configuration(); + + final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("hibernate-mysql.properties"); + final Properties hibernateProperties = new Properties(); + hibernateProperties.load(inputStream); + configuration.setProperties(hibernateProperties); + + configuration.addAnnotatedClass(Foo.class); + + final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); + final SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + final Session session = sessionFactory.openSession(); + session.beginTransaction(); + session.saveOrUpdate(new Foo()); + session.getTransaction().commit(); + } + +}