Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
ce91d77866
|
@ -30,6 +30,11 @@
|
|||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||
|
@ -39,5 +44,6 @@
|
|||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
default.configuration=
|
||||
eclipse.preferences.version=1
|
||||
hibernate3.enabled=true
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
<!-- Generated 29-abr-2014 15:39:59 by Hibernate Tools 3.4.0.CR1 -->
|
||||
<hibernate-mapping>
|
||||
<class name="org.baeldung.persistence.model.Bar" table="BAR">
|
||||
<id name="id" type="int">
|
||||
<column name="ID" />
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
<set name="fooSet" table="FOO" inverse="false" lazy="true" order-by="NAME DESC">
|
||||
<key>
|
||||
<column name="BAR_ID" />
|
||||
</key>
|
||||
<one-to-many class="org.baeldung.persistence.model.Foo" />
|
||||
</set>
|
||||
<property name="name" type="java.lang.String">
|
||||
<column name="NAME" />
|
||||
</property>
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -1,25 +1,34 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedQuery;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.hibernate.annotations.OrderBy;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
@Entity
|
||||
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
|
||||
public class Bar implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private int id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
private List<Foo> foos;
|
||||
@OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
@OrderBy(clause = "NAME DESC")
|
||||
private Set<Foo> fooSet = Sets.newHashSet();
|
||||
|
||||
public Bar() {
|
||||
super();
|
||||
|
@ -33,11 +42,19 @@ public class Bar implements Serializable {
|
|||
|
||||
// API
|
||||
|
||||
public long getId() {
|
||||
public Set<Foo> getFooSet() {
|
||||
return fooSet;
|
||||
}
|
||||
|
||||
public void setFooSet(final Set<Foo> fooSet) {
|
||||
this.fooSet = fooSet;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -49,10 +66,6 @@ public class Bar implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Foo> getFooList() {
|
||||
return foos;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
|
@ -83,7 +96,7 @@ public class Bar implements Serializable {
|
|||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Foo [name=").append(name).append("]");
|
||||
builder.append("Bar [name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
<!-- Generated 23-abr-2014 18:00:30 by Hibernate Tools 3.4.0.CR1 -->
|
||||
<hibernate-mapping>
|
||||
<class name="org.baeldung.persistence.model.Foo" table="FOO">
|
||||
<id name="id" type="int">
|
||||
<column name="ID" />
|
||||
<generator class="native" />
|
||||
</id>
|
||||
<property name="name" type="java.lang.String">
|
||||
<column name="NAME" />
|
||||
</property>
|
||||
<many-to-one name="bar" class="org.baeldung.persistence.model.Bar" fetch="join" >
|
||||
<column name="BAR_ID" />
|
||||
</many-to-one>
|
||||
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -7,6 +7,11 @@ import javax.persistence.Entity;
|
|||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
|
@ -18,17 +23,29 @@ public class Foo implements Serializable {
|
|||
@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();
|
||||
}
|
||||
|
||||
public Foo(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// API
|
||||
//
|
||||
|
||||
public Bar getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(final Bar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
|
||||
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<!-- Database connection settings -->
|
||||
<property name="connection.driver_class">com.mysql.jdbc.Driver</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>
|
||||
|
||||
<!-- SQL dialect -->
|
||||
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
|
||||
|
||||
<!-- Enable Hibernate's automatic session context management -->
|
||||
<property name="current_session_context_class">thread</property>
|
||||
|
||||
<!-- Disable the second-level cache -->
|
||||
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
|
||||
|
||||
<!-- Echo all executed SQL to stdout -->
|
||||
<property name="show_sql">true</property>
|
||||
|
||||
<!-- Drop and re-create the database schema on startup -->
|
||||
|
||||
<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,21 @@
|
|||
package org.baeldung.persistence;
|
||||
|
||||
import org.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest;
|
||||
import org.baeldung.persistence.hibernate.FooSortingPersistenceServiceTest;
|
||||
import org.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest;
|
||||
import org.baeldung.persistence.service.FooServicePersistenceIntegrationTest;
|
||||
import org.baeldung.persistence.service.ParentServicePersistenceIntegrationTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({// @formatter:off
|
||||
FooServiceBasicPersistenceIntegrationTest.class
|
||||
,FooPaginationPersistenceIntegrationTest.class
|
||||
,FooServicePersistenceIntegrationTest.class
|
||||
,ParentServicePersistenceIntegrationTest.class
|
||||
,FooSortingPersistenceServiceTest.class
|
||||
}) // @formatter:on
|
||||
public class IntegrationTestSuite {
|
||||
//
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.persistence.service;
|
||||
package org.baeldung.persistence.hibernate;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
@ -8,6 +8,7 @@ import static org.junit.Assert.assertThat;
|
|||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Query;
|
||||
|
@ -30,7 +31,7 @@ import com.google.common.collect.Lists;
|
|||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class FooServicePaginationPersistenceIntegrationTest {
|
||||
public class FooPaginationPersistenceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
|
@ -0,0 +1,169 @@
|
|||
package org.baeldung.persistence.hibernate;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.NullPrecedence;
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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)
|
||||
@SuppressWarnings("unchecked")
|
||||
public class FooSortingPersistenceServiceTest {
|
||||
private SessionFactory sf;
|
||||
private Session sess;
|
||||
|
||||
@Before
|
||||
public final 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() {
|
||||
sess.getTransaction().commit();
|
||||
}
|
||||
|
||||
@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();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
assertNull(fooList.get(0).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("name"));
|
||||
criteria.addOrder(Order.asc("id"));
|
||||
final List<Foo> fooList = criteria.list();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
assertNull(fooList.get(0).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@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();
|
||||
for (final Bar bar : barList) {
|
||||
final Set<Foo> fooSet = bar.getFooSet();
|
||||
System.out.println("Bar Id:" + bar.getId());
|
||||
for (final Foo foo : fooSet) {
|
||||
System.out.println("FooName:" + foo.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
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)));
|
||||
}
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package org.baeldung.persistence.service;
|
|||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
|
|
@ -1,390 +0,0 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.NullPrecedence;
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class FooSortingServiceTest {
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
||||
Session sess = null;
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Foo f ORDER BY f.name";
|
||||
final Query query = sess.createQuery(hql);
|
||||
fooList = query.list();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
||||
Session sess = null;
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Foo f ORDER BY f.name ASC";
|
||||
final Query query = sess.createQuery(hql);
|
||||
fooList = query.list();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
||||
Session sess = null;
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Foo f ORDER BY f.name, f.id";
|
||||
final Query query = sess.createQuery(hql);
|
||||
fooList = query.list();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedOrderedResults() {
|
||||
Session sess = null;
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
||||
final Query query = sess.createQuery(hql);
|
||||
fooList = query.list();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
||||
Session sess = null;
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
try {
|
||||
sess.beginTransaction();
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("id"));
|
||||
fooList = criteria.list();
|
||||
assertEquals(1, fooList.get(0).getId());
|
||||
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
|
||||
sess.getTransaction().commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenCriteriaSortingByMultipAttr_thenSortedResults() {
|
||||
Session sess = null;
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
try {
|
||||
sess.beginTransaction();
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("name"));
|
||||
criteria.addOrder(Order.asc("id"));
|
||||
fooList = criteria.list();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
|
||||
sess.getTransaction().commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
||||
Session sess = null;
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
try {
|
||||
sess.beginTransaction();
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
||||
fooList = criteria.list();
|
||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
sess.getTransaction().commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
||||
Session sess = null;
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
try {
|
||||
sess.beginTransaction();
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||
fooList = criteria.list();
|
||||
assertNull(fooList.get(0).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||
}
|
||||
sess.getTransaction().commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenHQlSortingByStringNullLast_thenLastNull() {
|
||||
Session sess = null;
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
||||
|
||||
final Query query = sess.createQuery(hql);
|
||||
fooList = query.list();
|
||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||
}
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
||||
Session sess = null;
|
||||
final Set<Foo> fooList = new TreeSet();
|
||||
List<Bar> barList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Bar b ORDER BY b.id";
|
||||
final Query query = sess.createQuery(hql);
|
||||
barList = query.list();
|
||||
|
||||
for (final Bar bar : barList) {
|
||||
System.out.println("Bar Id:" + bar.getId());
|
||||
for (final Foo foo : bar.getFooList()) {
|
||||
System.out.println("FooName:" + foo.getName());
|
||||
}
|
||||
}
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public final void whenSortingPrimitiveNulls_thenException() {
|
||||
// Session sess = null;
|
||||
// List<Foo> fooList = new ArrayList();
|
||||
// final List<Bar> barList = new ArrayList();
|
||||
//
|
||||
// try {
|
||||
// final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
// sess = sf.openSession();
|
||||
// final String hql = "FROM Foo f ORDER BY f.idx";
|
||||
// final Query query = sess.createQuery(hql);
|
||||
// boolean thrown = false;
|
||||
// try {
|
||||
// fooList = criteria.list();
|
||||
// } catch (final org.hibernate.PropertyAccessException e) {
|
||||
// thrown = true;
|
||||
// }
|
||||
// assertTrue(thrown);
|
||||
//
|
||||
// final Transaction tr = sess.beginTransaction();
|
||||
// tr.commit();
|
||||
// } catch (final Exception ex) {
|
||||
// ex.printStackTrace();
|
||||
// } finally {
|
||||
// if (sess != null) {
|
||||
// sess.close();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@Test
|
||||
public final void whenSortingStringNullsLast_thenReturnNullsLast() {
|
||||
Session sess = null;
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
final List<Bar> barList = Lists.newArrayList();
|
||||
|
||||
try {
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
||||
final Query query = sess.createQuery(hql);
|
||||
fooList = query.list();
|
||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("FooIDX:" + foo.getName());
|
||||
|
||||
}
|
||||
|
||||
final Transaction tr = sess.beginTransaction();
|
||||
tr.commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenNullPrimitiveValueCriteriaSortingByMultipAttr_thenException() {
|
||||
Session sess = null;
|
||||
final SessionFactory sf = new Configuration().configure().buildSessionFactory();
|
||||
sess = sf.openSession();
|
||||
List<Foo> fooList = Lists.newArrayList();
|
||||
try {
|
||||
sess.beginTransaction();
|
||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||
criteria.addOrder(Order.asc("idx"));
|
||||
boolean thrown = false;
|
||||
try {
|
||||
fooList = criteria.list();
|
||||
} catch (final org.hibernate.PropertyAccessException e) {
|
||||
thrown = true;
|
||||
}
|
||||
assertTrue(thrown);
|
||||
|
||||
sess.getTransaction().commit();
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (sess != null) {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,11 +22,7 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
|
|
@ -30,6 +30,11 @@
|
|||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.springframework.ide.eclipse.core.springnature</nature>
|
||||
|
@ -39,5 +44,6 @@
|
|||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
default.configuration=
|
||||
eclipse.preferences.version=1
|
||||
hibernate3.enabled=true
|
|
@ -3,14 +3,20 @@ package org.baeldung.persistence.model;
|
|||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.OrderBy;
|
||||
|
||||
@Entity
|
||||
public class Bar implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
@ -19,7 +25,11 @@ public class Bar implements Serializable {
|
|||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
private List<Foo> foos;
|
||||
|
||||
@OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
@OrderBy("name ASC")
|
||||
List<Foo> fooList;
|
||||
|
||||
|
||||
public Bar() {
|
||||
super();
|
||||
|
@ -38,6 +48,7 @@ public class Bar implements Serializable {
|
|||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -50,7 +61,11 @@ public class Bar implements Serializable {
|
|||
}
|
||||
|
||||
public List<Foo> getFooList() {
|
||||
return foos;
|
||||
return fooList;
|
||||
}
|
||||
|
||||
public void setFooList(final List<Foo> fooList) {
|
||||
this.fooList = fooList;
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -83,7 +98,7 @@ public class Bar implements Serializable {
|
|||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append("Foo [name=").append(name).append("]");
|
||||
builder.append("Bar [name=").append(name).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -4,19 +4,17 @@ import java.io.Serializable;
|
|||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
|
@ -28,13 +26,30 @@ public class Foo implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
// API
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "ID")
|
||||
private long id;
|
||||
@Column(name = "NAME")
|
||||
private String name;
|
||||
|
||||
@ManyToOne(targetEntity = Bar.class, fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "BAR_ID")
|
||||
private Bar bar;
|
||||
|
||||
public Bar getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(final Bar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
@ -46,8 +61,6 @@ public class Foo implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
|
||||
<persistence-unit name="punit" >
|
||||
<class>org.baeldung.persistence.model.Foo</class>
|
||||
<class>org.baeldung.persistence.model.Bar</class>
|
||||
<properties>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/HIBERTEST"/>
|
||||
<property name="javax.persistence.ddl-generation" value="drop-and-create-tables"/>
|
||||
<property name="javax.persistence.logging.level" value="INFO"/>
|
||||
<property name = "hibernate.show_sql" value = "true" />
|
||||
</properties>
|
||||
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -1,8 +1,6 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -18,35 +16,31 @@ import javax.persistence.criteria.Root;
|
|||
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.After;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class FooServiceSortingTests {
|
||||
private EntityManager entityManager;
|
||||
private static EntityManager entityManager;
|
||||
private static EntityManagerFactory emf;
|
||||
private static EntityTransaction entityTransaction;
|
||||
private static CriteriaBuilder criteriaBuilder;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
//
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() {
|
||||
//
|
||||
emf = Persistence.createEntityManagerFactory("punit");
|
||||
entityManager = emf.createEntityManager();
|
||||
entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
|
||||
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.id";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
assertEquals(1, fooList.get(0).getId());
|
||||
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||
}
|
||||
|
@ -54,16 +48,9 @@ public class FooServiceSortingTests {
|
|||
|
||||
@Test
|
||||
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
|
||||
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.id desc";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
assertEquals(100, fooList.get(0).getId());
|
||||
assertEquals(1, fooList.get(fooList.toArray().length - 1).getId());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||
}
|
||||
|
@ -71,10 +58,6 @@ public class FooServiceSortingTests {
|
|||
|
||||
@Test
|
||||
public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.name asc, f.id desc";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
|
@ -84,13 +67,18 @@ public class FooServiceSortingTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select b from Bar as b order by b.id";
|
||||
public final void whenSortingFooByBar_thenBarsSorted() {
|
||||
final String jql = "Select f from Foo as f order by f.name, f.bar.id";
|
||||
final Query barJoinQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = barJoinQuery.getResultList();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------BarId:" + foo.getBar().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
|
||||
final String jql = "Select b from Bar as b order by b.id";
|
||||
final Query barQuery = entityManager.createQuery(jql);
|
||||
final List<Bar> barList = barQuery.getResultList();
|
||||
for (final Bar bar : barList) {
|
||||
|
@ -103,11 +91,6 @@ public class FooServiceSortingTests {
|
|||
|
||||
@Test
|
||||
public final void whenSortingByStringNullLast_thenLastNull() {
|
||||
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
|
@ -117,43 +100,32 @@ public class FooServiceSortingTests {
|
|||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public final void whenSortingByStringNullFirst_thenFirstNull() {
|
||||
// final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
// final EntityManager entityManager = emf.createEntityManager();
|
||||
// final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
// entityTransaction.begin();
|
||||
// final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
|
||||
// final Query sortQuery = entityManager.createQuery(jql);
|
||||
// final List<Foo> fooList = sortQuery.getResultList();
|
||||
// assertNull(fooList.get(0).getName());
|
||||
// for (final Foo foo : fooList) {
|
||||
// System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null());
|
||||
// }
|
||||
// }
|
||||
|
||||
@Test
|
||||
public final void whenSortingByIntNull_thenException() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
|
||||
public final void whenSortingByStringNullFirst_thenFirstNull() {
|
||||
final Foo nullNameFoo = new Foo();
|
||||
nullNameFoo.setName(null);
|
||||
|
||||
final Bar bar = new Bar();
|
||||
final List<Foo> fooList1 = Lists.newArrayList();
|
||||
bar.setName("Bar_Me");
|
||||
nullNameFoo.setBar(bar);
|
||||
fooList1.add(nullNameFoo);
|
||||
bar.setFooList(fooList1);
|
||||
entityManager.persist(bar);
|
||||
entityManager.persist(nullNameFoo);
|
||||
entityTransaction.commit();
|
||||
final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
boolean thrown = false;
|
||||
try {
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
} catch (final javax.persistence.PersistenceException e) {
|
||||
thrown = true;
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
assertNull(fooList.get(0).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName());
|
||||
}
|
||||
assertTrue(thrown);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||
|
@ -163,14 +135,11 @@ public class FooServiceSortingTests {
|
|||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||
|
@ -182,4 +151,5 @@ public class FooServiceSortingTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue