initial compilation fixes

This commit is contained in:
eugenp 2014-05-10 15:33:07 +03:00
parent 8f86fe971a
commit a5c1d774ec
3 changed files with 43 additions and 58 deletions

View File

@ -1,26 +1,13 @@
package org.baeldung.persistence.service; package org.baeldung.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.junit.Assert.assertNull;
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;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.After;
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.Foo;
import org.baeldung.spring.PersistenceConfig;
import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.NullPrecedence; import org.hibernate.NullPrecedence;
import org.hibernate.Query; import org.hibernate.Query;
@ -29,8 +16,13 @@ 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.hibernate.criterion.Order;
import com.cc.example.hibernate.Foo; import org.junit.After;
import com.cc.example.hibernate.Bar; import org.junit.BeforeClass;
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) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)

View File

@ -1,66 +1,70 @@
package org.baeldung.persistence.model; package org.baeldung.persistence.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.CascadeType; import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
@Entity @Entity
public class Bar implements Serializable { public class Bar implements Serializable {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long id; private long id;
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("name ASC") @OrderBy("name ASC")
List<Foo> fooList; List<Foo> fooList;
public Bar() { public Bar() {
super(); super();
} }
public Bar(final String name) { public Bar(final String name) {
super(); super();
this.name = name; this.name = name;
} }
// API // API
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;
} }
public List<Foo> getFooList() { public List<Foo> getFooList() {
return fooList; return fooList;
} }
public void setFooList(final List<Foo> fooList) { public void setFooList(final List<Foo> fooList) {
this.fooList = fooList; this.fooList = fooList;
} }
// //
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@ -68,7 +72,7 @@ public class Bar implements Serializable {
result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode());
return result; return result;
} }
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
if (this == obj) if (this == obj)
@ -85,12 +89,12 @@ public class Bar implements Serializable {
return false; return false;
return true; return true;
} }
@Override @Override
public String toString() { public String toString() {
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
builder.append("Bar [name=").append(name).append("]"); builder.append("Bar [name=").append(name).append("]");
return builder.toString(); return builder.toString();
} }
} }

View File

@ -1,7 +1,9 @@
package org.baeldung.persistence.service; package org.baeldung.persistence.service;
import static org.junit.Assert.*; import static org.junit.Assert.assertNull;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
@ -11,11 +13,13 @@ import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import com.google.common.collect.Lists;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.cc.jpa.example.Foo;
import com.cc.jpa.example.Bar; import com.google.common.collect.Lists;
public class FooServiceSortingTests { public class FooServiceSortingTests {
private static EntityManager entityManager; private static EntityManager entityManager;
@ -25,7 +29,6 @@ public class FooServiceSortingTests {
@BeforeClass @BeforeClass
public static void before() { public static void before() {
emf = Persistence.createEntityManagerFactory("punit"); emf = Persistence.createEntityManagerFactory("punit");
entityManager = emf.createEntityManager(); entityManager = emf.createEntityManager();
entityTransaction = entityManager.getTransaction(); entityTransaction = entityManager.getTransaction();
@ -35,43 +38,36 @@ public class FooServiceSortingTests {
@Test @Test
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
final String jql = "Select f from Foo as f order by f.id"; final String jql = "Select f from Foo as f order by f.id";
final Query sortQuery = entityManager.createQuery(jql); final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList(); final List<Foo> fooList = sortQuery.getResultList();
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());
} }
} }
@Test @Test
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
final String jql = "Select f from Foo as f order by f.id desc"; final String jql = "Select f from Foo as f order by f.id desc";
final Query sortQuery = entityManager.createQuery(jql); final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList(); final List<Foo> fooList = sortQuery.getResultList();
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());
} }
} }
@Test @Test
public final void whenSortingByTwoAttributes_thenPrintSortedResult() { public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
final String jql = "Select f from Foo as f order by f.name asc, f.id desc"; final String jql = "Select f from Foo as f order by f.name asc, f.id desc";
final Query sortQuery = entityManager.createQuery(jql); final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList(); final List<Foo> fooList = sortQuery.getResultList();
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());
} }
} }
@Test @Test
public final void whenSortingFooByBar_thenBarsSorted() { public final void whenSortingFooByBar_thenBarsSorted() {
final String jql = "Select f from Foo as f order by f.name, f.bar.id"; final String jql = "Select f from Foo as f order by f.name, f.bar.id";
final Query barJoinQuery = entityManager.createQuery(jql); final Query barJoinQuery = entityManager.createQuery(jql);
final List<Foo> fooList = barJoinQuery.getResultList(); final List<Foo> fooList = barJoinQuery.getResultList();
@ -82,7 +78,6 @@ public class FooServiceSortingTests {
@Test @Test
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() { public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
final String jql = "Select b from Bar as b order by b.id"; final String jql = "Select b from Bar as b order by b.id";
final Query barQuery = entityManager.createQuery(jql); final Query barQuery = entityManager.createQuery(jql);
final List<Bar> barList = barQuery.getResultList(); final List<Bar> barList = barQuery.getResultList();
@ -92,12 +87,10 @@ public class FooServiceSortingTests {
System.out.println("FooName:" + foo.getName()); System.out.println("FooName:" + foo.getName());
} }
} }
} }
@Test @Test
public final void whenSortingByStringNullLast_thenLastNull() { public final void whenSortingByStringNullLast_thenLastNull() {
final String jql = "Select f from Foo as f order by f.name desc NULLS LAST"; final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
final Query sortQuery = entityManager.createQuery(jql); final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList(); final List<Foo> fooList = sortQuery.getResultList();
@ -109,7 +102,6 @@ public class FooServiceSortingTests {
@Test @Test
public final void whenSortingByStringNullFirst_thenFirstNull() { public final void whenSortingByStringNullFirst_thenFirstNull() {
final Foo nullNameFoo = new Foo(); final Foo nullNameFoo = new Foo();
nullNameFoo.setName(null); nullNameFoo.setName(null);
@ -133,7 +125,6 @@ public class FooServiceSortingTests {
@Test @Test
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() { public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
criteriaBuilder = entityManager.getCriteriaBuilder(); criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class); final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
final Root<Foo> from = criteriaQuery.from(Foo.class); final Root<Foo> from = criteriaQuery.from(Foo.class);
@ -144,12 +135,10 @@ public class FooServiceSortingTests {
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());
} }
} }
@Test @Test
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() { public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
criteriaBuilder = entityManager.getCriteriaBuilder(); criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class); final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
final Root<Foo> from = criteriaQuery.from(Foo.class); final Root<Foo> from = criteriaQuery.from(Foo.class);