Merge pull request #9 from egmp777/master

Corrections After the Merge Today
This commit is contained in:
Eugen 2014-05-11 09:59:08 +03:00
commit 5e56ba8e7c
3 changed files with 138 additions and 134 deletions

View File

@ -2,10 +2,20 @@ package org.baeldung.persistence.model;
import java.io.Serializable;
import java.util.Set;
import com.google.common.collect.Sets;
import javax.persistence.*;
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 {
@ -16,7 +26,7 @@ public class Bar implements Serializable {
public Bar(final String name) {
super();
this.name = name;
}
@ -31,18 +41,18 @@ public class Bar implements Serializable {
@OrderBy(clause = "NAME DESC")
Set<Foo> fooSet = Sets.newHashSet();
//API
// API
public Set<Foo> getFooSet() {
return fooSet;
}
public void setFooSet(final Set<Foo> fooSet) {
this.fooSet = fooSet;
}
public int getId() {
return this.id;
return id;
}
public void setId(final int id) {
@ -50,14 +60,15 @@ public class Bar implements Serializable {
}
public String getName() {
return this.name;
return name;
}
public void setName(final String name) {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
@ -65,7 +76,7 @@ public class Bar implements Serializable {
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
@ -82,7 +93,7 @@ public class Bar implements Serializable {
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
@ -90,5 +101,4 @@ public class Bar implements Serializable {
return builder.toString();
}
}

View File

@ -21,6 +21,12 @@ public class Foo implements Serializable {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ -38,25 +44,18 @@ public class Foo implements Serializable {
this.bar = bar;
}
public int getBar_Id() {
return bar_Id;
}
public void setBar_Id(final int bar_Id) {
this.bar_Id = bar_Id;
}
private int bar_Id;
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}

View File

@ -1,21 +1,16 @@
package org.baeldung.persistence.service;
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.BeforeClass;
import org.junit.Test;
@ -49,138 +44,138 @@ public class FooSortingServiceTest {
}
@Test
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
/* @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());
}
sess.getTransaction().commit();
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());
}
sess.getTransaction().commit();
}
}
@Test
public final void whenHQlSortingByStringNullLast_thenLastNull() {
@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());
}
sess.getTransaction().commit();
}
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());
}
sess.getTransaction().commit();
}
@Test
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
@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());
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());
}
sess.getTransaction().commit();
}
}
sess.getTransaction().commit();
}
@Test
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
@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()
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()
);
}
sess.getTransaction().commit();
}
);
}
sess.getTransaction().commit();
}
@Test
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
@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()
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()
);
}
sess.getTransaction().commit();
}
);
}
sess.getTransaction().commit();
}
@Test
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
@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());
}
sess.getTransaction().commit();
}
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());
}
sess.getTransaction().commit();
}
@Test
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
@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());
}
sess.getTransaction().commit();
}
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());
}
sess.getTransaction().commit();
}
@Test
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
@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());
}
sess.getTransaction().commit();
}
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());
}
sess.getTransaction().commit();
}
@Test
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
@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());
}
sess.getTransaction().commit();
}
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());
}
sess.getTransaction().commit();
}
@Test
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
@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());
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());
}
sess.getTransaction().commit();
}
sess.getTransaction().commit();
}
}*/
@Test
public final void whenSortingBars_thenBarsWithSortedFoos() {