File tree Expand file tree Collapse file tree
java/org/baeldung/ex/mappingexception/cause4/persistence/model
test/java/org/baeldung/ex/mappingexception Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception .cause4 .persistence .model ;
2+
3+ import java .io .Serializable ;
4+
5+ import javax .persistence .Entity ;
6+ import javax .persistence .GeneratedValue ;
7+ import javax .persistence .GenerationType ;
8+ import javax .persistence .Id ;
9+
10+ @ Entity
11+ public class Foo implements Serializable {
12+
13+ @ Id
14+ @ GeneratedValue (strategy = GenerationType .AUTO )
15+ private long id ;
16+
17+ public Foo () {
18+ super ();
19+ }
20+
21+ // API
22+
23+ public long getId () {
24+ return id ;
25+ }
26+
27+ public void setId (final long id ) {
28+ this .id = id ;
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ hibernate.connection.username =tutorialuser
2+ hibernate.connection.password =tutorialmy5ql
3+ hibernate.connection.driver_class =com.mysql.jdbc.Driver
4+ hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
5+ # hibernate.connection.datasource=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
6+ hibernate.connection.url =jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
7+ hibernate.show_sql =false
8+ hibernate.hbm2ddl.auto =create
Original file line number Diff line number Diff line change 1+ package org .baeldung .ex .mappingexception ;
2+
3+ import java .io .IOException ;
4+ import java .io .InputStream ;
5+ import java .util .Properties ;
6+
7+ import org .baeldung .ex .mappingexception .cause4 .persistence .model .Foo ;
8+ import org .hibernate .Session ;
9+ import org .hibernate .SessionFactory ;
10+ import org .hibernate .cfg .Configuration ;
11+ import org .hibernate .service .ServiceRegistry ;
12+ import org .hibernate .service .ServiceRegistryBuilder ;
13+ import org .junit .Test ;
14+
15+ public class Cause4MappingExceptionIntegrationTest {
16+
17+ // tests
18+
19+ @ Test
20+ public final void givenEntityIsPersisted_thenException () throws IOException {
21+ final Configuration configuration = new Configuration ();
22+
23+ final InputStream inputStream = this .getClass ().getClassLoader ().getResourceAsStream ("hibernate-mysql.properties" );
24+ final Properties hibernateProperties = new Properties ();
25+ hibernateProperties .load (inputStream );
26+ configuration .setProperties (hibernateProperties );
27+
28+ configuration .addAnnotatedClass (Foo .class );
29+
30+ final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder ().applySettings (configuration .getProperties ()).buildServiceRegistry ();
31+ final SessionFactory sessionFactory = configuration .buildSessionFactory (serviceRegistry );
32+
33+ final Session session = sessionFactory .openSession ();
34+ session .beginTransaction ();
35+ session .saveOrUpdate (new Foo ());
36+ session .getTransaction ().commit ();
37+ }
38+
39+ }
You can’t perform that action at this time.
0 commit comments