minor cleanup
This commit is contained in:
parent
5e56ba8e7c
commit
a2c98ab37f
|
@ -20,6 +20,16 @@ import com.google.common.collect.Sets;
|
|||
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
|
||||
public class Bar implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OrderBy(clause = "NAME DESC")
|
||||
private Set<Foo> fooSet = Sets.newHashSet();
|
||||
|
||||
public Bar() {
|
||||
super();
|
||||
}
|
||||
|
@ -30,17 +40,6 @@ public class Bar implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OrderBy(clause = "NAME DESC")
|
||||
Set<Foo> fooSet = Sets.newHashSet();
|
||||
|
||||
// API
|
||||
|
||||
public Set<Foo> getFooSet() {
|
||||
|
|
|
@ -15,7 +15,5 @@
|
|||
<column name="BAR_ID" />
|
||||
</many-to-one>
|
||||
|
||||
|
||||
|
||||
</class>
|
||||
</hibernate-mapping>
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.baeldung.persistence.model;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
|
@ -15,7 +16,17 @@ import org.hibernate.annotations.FetchMode;
|
|||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@ManyToOne(targetEntity = Bar.class)
|
||||
@JoinColumn(name = "BAR_ID")
|
||||
@Fetch(FetchMode.JOIN)
|
||||
private Bar bar = new Bar();
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
|
@ -27,14 +38,7 @@ public class Foo implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private String name;
|
||||
@ManyToOne(targetEntity = Bar.class)
|
||||
@JoinColumn(name = "BAR_ID")
|
||||
@Fetch(FetchMode.JOIN)
|
||||
private Bar bar = new Bar();
|
||||
//
|
||||
|
||||
public Bar getBar() {
|
||||
return bar;
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
<session-factory>
|
||||
<!-- Database connection settings -->
|
||||
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
|
||||
<property name="connection.url">jdbc:mysql://localhost:3306/HIBERTEST2_TEST</property>
|
||||
<property name="connection.username">root</property>
|
||||
<property name="connection.password"></property>
|
||||
<property name="connection.url">jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true</property>
|
||||
<property name="connection.username">tutorialuser</property>
|
||||
<property name="connection.password">tutorialmy5ql</property>
|
||||
|
||||
<!-- JDBC connection pool (use the built-in) -->
|
||||
<property name="connection.pool_size">1</property>
|
||||
|
@ -33,5 +33,5 @@
|
|||
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml"/>
|
||||
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml"/>
|
||||
</session-factory>
|
||||
|
||||
|
||||
</hibernate-configuration>
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class FooServiceBasicPersistenceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@Autowired
|
||||
private IFooService fooService;
|
||||
|
||||
private Session session;
|
||||
|
||||
// tests
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
session = sessionFactory.openSession();
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenContextIsBootstrapped_thenNoExceptions() {
|
||||
//
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
fooService.create(new Foo(randomAlphabetic(6)));
|
||||
}
|
||||
|
||||
}
|
|
@ -12,7 +12,7 @@ import org.hibernate.SessionFactory;
|
|||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.junit.After;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
@ -21,32 +21,26 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
|
||||
|
||||
|
||||
public class FooSortingServiceTest {
|
||||
private static Configuration configuration;
|
||||
private static StandardServiceRegistryBuilder builder;
|
||||
private static SessionFactory sf;
|
||||
private static Session sess;
|
||||
private SessionFactory sf;
|
||||
private Session sess;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
|
||||
configuration = new Configuration().configure();
|
||||
builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
|
||||
@Before
|
||||
public void before() {
|
||||
final Configuration configuration = new Configuration().configure();
|
||||
final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
|
||||
sf = configuration.buildSessionFactory(builder.build());
|
||||
sess = sf.openSession();
|
||||
sess.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
/* @Test
|
||||
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
||||
|
||||
final String hql = "FROM Foo f ORDER BY f.name";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Foo> fooList = query.list();
|
||||
|
@ -54,12 +48,10 @@ public class FooSortingServiceTest {
|
|||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
sess.getTransaction().commit();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenHQlSortingByStringNullLast_thenLastNull() {
|
||||
|
||||
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Foo> fooList = query.list();
|
||||
|
@ -72,7 +64,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
|
||||
|
||||
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Foo> fooList = query.list();
|
||||
|
@ -86,7 +77,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
||||
|
||||
final String hql = "FROM Foo f ORDER BY f.name ASC";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Foo> fooList = query.list();
|
||||
|
@ -100,7 +90,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
||||
|
||||
final String hql = "FROM Foo f ORDER BY f.name, f.id";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Foo> fooList = query.list();
|
||||
|
@ -114,7 +103,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
||||
|
||||
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Foo> fooList = query.list();
|
||||
|
@ -126,7 +114,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
||||
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("id"));
|
||||
final List<Foo> fooList = criteria.list();
|
||||
|
@ -138,7 +125,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
||||
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("name"));
|
||||
criteria.addOrder(Order.asc("id"));
|
||||
|
@ -151,7 +137,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
||||
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
||||
final List<Foo> fooList = criteria.list();
|
||||
|
@ -164,7 +149,6 @@ public class FooSortingServiceTest {
|
|||
|
||||
@Test
|
||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
||||
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||
final List<Foo> fooList = criteria.list();
|
||||
|
@ -174,12 +158,10 @@ public class FooSortingServiceTest {
|
|||
|
||||
}
|
||||
sess.getTransaction().commit();
|
||||
|
||||
}*/
|
||||
|
||||
@Test
|
||||
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
||||
|
||||
final String hql = "FROM Bar b ORDER BY b.id";
|
||||
final Query query = sess.createQuery(hql);
|
||||
final List<Bar> barList = query.list();
|
||||
|
@ -192,7 +174,6 @@ public class FooSortingServiceTest {
|
|||
}
|
||||
}
|
||||
sess.getTransaction().commit();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue