Corrections After the Merge Today
This commit is contained in:
parent
79f00d975f
commit
ade8cb51db
@ -2,10 +2,20 @@ package org.baeldung.persistence.model;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Set;
|
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 org.hibernate.annotations.OrderBy;
|
||||||
|
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
|
@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b")
|
||||||
public class Bar implements Serializable {
|
public class Bar implements Serializable {
|
||||||
@ -31,7 +41,7 @@ public class Bar implements Serializable {
|
|||||||
@OrderBy(clause = "NAME DESC")
|
@OrderBy(clause = "NAME DESC")
|
||||||
Set<Foo> fooSet = Sets.newHashSet();
|
Set<Foo> fooSet = Sets.newHashSet();
|
||||||
|
|
||||||
//API
|
// API
|
||||||
|
|
||||||
public Set<Foo> getFooSet() {
|
public Set<Foo> getFooSet() {
|
||||||
return fooSet;
|
return fooSet;
|
||||||
@ -42,7 +52,7 @@ public class Bar implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return this.id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(final int id) {
|
public void setId(final int id) {
|
||||||
@ -50,12 +60,13 @@ public class Bar implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(final String name) {
|
public void setName(final String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -90,5 +101,4 @@ public class Bar implements Serializable {
|
|||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,12 @@ public class Foo implements Serializable {
|
|||||||
super();
|
super();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Foo(final String name) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
private long id;
|
private long id;
|
||||||
@ -38,25 +44,18 @@ public class Foo implements Serializable {
|
|||||||
this.bar = bar;
|
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() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(final long id) {
|
public void setId(final long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(final String name) {
|
public void setName(final String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,16 @@
|
|||||||
package org.baeldung.persistence.service;
|
package org.baeldung.persistence.service;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Bar;
|
import org.baeldung.persistence.model.Bar;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.persistence.model.Foo;
|
||||||
import org.baeldung.spring.PersistenceConfig;
|
import org.baeldung.spring.PersistenceConfig;
|
||||||
import org.hibernate.Criteria;
|
|
||||||
import org.hibernate.NullPrecedence;
|
|
||||||
import org.hibernate.Query;
|
import org.hibernate.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.criterion.Order;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -49,138 +44,138 @@ public class FooSortingServiceTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
/* @Test
|
||||||
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() {
|
||||||
|
|
||||||
final String hql = "FROM Foo f ORDER BY f.name";
|
final String hql = "FROM Foo f ORDER BY f.name";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByStringNullLast_thenLastNull() {
|
public final void whenHQlSortingByStringNullLast_thenLastNull() {
|
||||||
|
|
||||||
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
final String hql = "FROM Foo f ORDER BY f.name NULLS LAST";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
|
public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {
|
||||||
|
|
||||||
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
|
final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
assertNull(fooList.get(0).getName());
|
assertNull(fooList.get(0).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name:" + foo.getName());
|
System.out.println("Name:" + foo.getName());
|
||||||
|
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() {
|
||||||
|
|
||||||
final String hql = "FROM Foo f ORDER BY f.name ASC";
|
final String hql = "FROM Foo f ORDER BY f.name ASC";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
|
||||||
|
|
||||||
final String hql = "FROM Foo f ORDER BY f.name, f.id";
|
final String hql = "FROM Foo f ORDER BY f.name, f.id";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() {
|
||||||
|
|
||||||
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC";
|
||||||
final Query query = sess.createQuery(hql);
|
final Query query = sess.createQuery(hql);
|
||||||
final List<Foo> fooList = query.list();
|
final List<Foo> fooList = query.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
|
||||||
|
|
||||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.asc("id"));
|
criteria.addOrder(Order.asc("id"));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
|
||||||
|
|
||||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.asc("name"));
|
criteria.addOrder(Order.asc("name"));
|
||||||
criteria.addOrder(Order.asc("id"));
|
criteria.addOrder(Order.asc("id"));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
|
||||||
|
|
||||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() {
|
||||||
|
|
||||||
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
final Criteria criteria = sess.createCriteria(Foo.class, "FOO");
|
||||||
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST));
|
||||||
final List<Foo> fooList = criteria.list();
|
final List<Foo> fooList = criteria.list();
|
||||||
assertNull(fooList.get(0).getName());
|
assertNull(fooList.get(0).getName());
|
||||||
for (final Foo foo : fooList) {
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
|
||||||
|
|
||||||
}
|
}
|
||||||
sess.getTransaction().commit();
|
sess.getTransaction().commit();
|
||||||
|
|
||||||
}
|
}*/
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
public final void whenSortingBars_thenBarsWithSortedFoos() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user