new persistence work
This commit is contained in:
parent
2fcc1187e2
commit
ec9b16694e
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue