[BAEL-3936] Formatted code
This commit is contained in:
parent
5a56276893
commit
9acc69220b
@ -11,31 +11,34 @@ import java.util.Objects;
|
|||||||
@Table(name = "cocktails")
|
@Table(name = "cocktails")
|
||||||
public class Cocktail {
|
public class Cocktail {
|
||||||
@Id
|
@Id
|
||||||
@Column(name="cocktail_name")
|
@Column(name = "cocktail_name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column
|
@Column
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
@Column(name="category")
|
@Column(name = "category")
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
@OneToOne
|
@OneToOne
|
||||||
@NotFound(action = NotFoundAction.IGNORE)
|
@NotFound(action = NotFoundAction.IGNORE)
|
||||||
@JoinColumn(name = "cocktail_name",
|
@JoinColumn(name = "cocktail_name",
|
||||||
referencedColumnName = "cocktail",
|
referencedColumnName = "cocktail",
|
||||||
insertable = false,
|
insertable = false, updatable = false,
|
||||||
updatable = false,
|
foreignKey = @javax.persistence
|
||||||
foreignKey = @javax.persistence.ForeignKey(value= ConstraintMode.NO_CONSTRAINT))
|
.ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
|
||||||
|
)
|
||||||
private Recipe recipe;
|
private Recipe recipe;
|
||||||
|
|
||||||
@OneToMany
|
@OneToMany
|
||||||
@NotFound(action=NotFoundAction.IGNORE)
|
@NotFound(action = NotFoundAction.IGNORE)
|
||||||
@JoinColumn(name = "cocktail",
|
@JoinColumn(
|
||||||
referencedColumnName = "cocktail_name",
|
name = "cocktail",
|
||||||
insertable = false,
|
referencedColumnName = "cocktail_name",
|
||||||
updatable = false,
|
insertable = false,
|
||||||
foreignKey = @javax.persistence.ForeignKey(value= ConstraintMode.NO_CONSTRAINT))
|
updatable = false,
|
||||||
|
foreignKey = @javax.persistence
|
||||||
|
.ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
|
||||||
private List<MultipleRecipe> recipeList;
|
private List<MultipleRecipe> recipeList;
|
||||||
|
|
||||||
public Cocktail() {
|
public Cocktail() {
|
||||||
@ -69,12 +72,14 @@ public class Cocktail {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
Cocktail cocktail = (Cocktail) o;
|
Cocktail cocktail = (Cocktail) o;
|
||||||
return Double.compare(cocktail.price, price) == 0 &&
|
return Double.compare(cocktail.price, price) == 0 &&
|
||||||
Objects.equals(name, cocktail.name) &&
|
Objects.equals(name, cocktail.name) &&
|
||||||
Objects.equals(category, cocktail.category);
|
Objects.equals(category, cocktail.category);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -7,28 +7,30 @@ import javax.persistence.Table;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="multiple_recipes")
|
@Table(name = "multiple_recipes")
|
||||||
public class MultipleRecipe {
|
public class MultipleRecipe {
|
||||||
@Id
|
@Id
|
||||||
@Column(name="id")
|
@Column(name = "id")
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(name="cocktail")
|
@Column(name = "cocktail")
|
||||||
private String cocktail;
|
private String cocktail;
|
||||||
|
|
||||||
@Column(name="instructions")
|
@Column(name = "instructions")
|
||||||
private String instructions;
|
private String instructions;
|
||||||
|
|
||||||
@Column(name="base_ingredient")
|
@Column(name = "base_ingredient")
|
||||||
private String baseIngredient;
|
private String baseIngredient;
|
||||||
|
|
||||||
public MultipleRecipe() {}
|
public MultipleRecipe() {
|
||||||
|
}
|
||||||
|
|
||||||
public MultipleRecipe(Long id, String cocktail, String instructions, String baseIngredient) {
|
public MultipleRecipe(Long id, String cocktail,
|
||||||
this.baseIngredient = baseIngredient;
|
String instructions, String baseIngredient) {
|
||||||
this.cocktail = cocktail;
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.cocktail = cocktail;
|
||||||
this.instructions = instructions;
|
this.instructions = instructions;
|
||||||
|
this.baseIngredient = baseIngredient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
@ -49,17 +51,20 @@ public class MultipleRecipe {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
MultipleRecipe that = (MultipleRecipe) o;
|
MultipleRecipe that = (MultipleRecipe) o;
|
||||||
return Objects.equals(id, that.id) &&
|
return Objects.equals(id, that.id) &&
|
||||||
Objects.equals(cocktail, that.cocktail) &&
|
Objects.equals(cocktail, that.cocktail) &&
|
||||||
Objects.equals(instructions, that.instructions) &&
|
Objects.equals(instructions, that.instructions) &&
|
||||||
Objects.equals(baseIngredient, that.baseIngredient);
|
Objects.equals(baseIngredient, that.baseIngredient);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, cocktail, instructions, baseIngredient);
|
return Objects.hash(id, cocktail,
|
||||||
|
instructions, baseIngredient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,10 @@ public class UnrelatedEntitiesUnitTest {
|
|||||||
entityManager.persist(mojito);
|
entityManager.persist(mojito);
|
||||||
entityManager.persist(ginTonic);
|
entityManager.persist(ginTonic);
|
||||||
entityManager.persist(new Recipe(mojito.getName(), "Some instructions"));
|
entityManager.persist(new Recipe(mojito.getName(), "Some instructions"));
|
||||||
entityManager.persist(new MultipleRecipe(1L, mojito.getName(), "some instructions", mojito.getCategory()));
|
entityManager.persist(new MultipleRecipe(1L, mojito.getName(),
|
||||||
entityManager.persist(new MultipleRecipe(2L, mojito.getName(), "some other instructions", mojito.getCategory()));
|
"some instructions", mojito.getCategory()));
|
||||||
|
entityManager.persist(new MultipleRecipe(2L, mojito.getName(),
|
||||||
|
"some other instructions", mojito.getCategory()));
|
||||||
entityManager.getTransaction().commit();
|
entityManager.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,114 +41,116 @@ public class UnrelatedEntitiesUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenQueryingForCocktailThatHasRecipe_thenTheExpectedCocktailReturned() {
|
public void whenQueryingForCocktailThatHasRecipe_thenTheExpectedCocktailReturned() {
|
||||||
// JPA
|
// JPA
|
||||||
Cocktail cocktail = entityManager.createQuery(
|
Cocktail cocktail = entityManager.createQuery("select c "
|
||||||
"select c from Cocktail c join c.recipe", Cocktail.class)
|
+ "from Cocktail c join c.recipe", Cocktail.class)
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
|
|
||||||
cocktail = entityManager.createQuery(
|
cocktail = entityManager.createQuery("select c "
|
||||||
"select c from Cocktail c join Recipe r on c.name = r.cocktail", Cocktail.class)
|
+ "from Cocktail c join Recipe r "
|
||||||
.getSingleResult();
|
+ "on c.name = r.cocktail", Cocktail.class)
|
||||||
|
.getSingleResult();
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
|
|
||||||
// QueryDSL
|
// QueryDSL
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail)
|
.join(QCocktail.cocktail.recipe)
|
||||||
.join(QCocktail.cocktail.recipe)
|
.fetchOne();
|
||||||
.fetchOne();
|
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
|
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail)
|
.join(QRecipe.recipe)
|
||||||
.join(QRecipe.recipe)
|
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
|
||||||
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
|
.fetchOne();
|
||||||
.fetchOne();
|
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenQueryingForCocktailThatHasNotARecipe_thenTheExpectedCocktailReturned() {
|
public void whenQueryingForCocktailThatHasNotARecipe_thenTheExpectedCocktailReturned() {
|
||||||
Cocktail cocktail = entityManager
|
Cocktail cocktail = entityManager.createQuery("select c "
|
||||||
.createQuery("select c from Cocktail c left join c.recipe r " +
|
+ "from Cocktail c left join c.recipe r "
|
||||||
"where r is null",
|
+ "where r is null", Cocktail.class)
|
||||||
Cocktail.class)
|
.getSingleResult();
|
||||||
.getSingleResult();
|
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
|
|
||||||
cocktail = entityManager
|
cocktail = entityManager.createQuery("select c "
|
||||||
.createQuery("select c from Cocktail c left join Recipe r " +
|
+ "from Cocktail c left join Recipe r "
|
||||||
"on c.name = r.cocktail " +
|
+ "on c.name = r.cocktail "
|
||||||
"where r is null",
|
+ "where r is null", Cocktail.class)
|
||||||
Cocktail.class)
|
.getSingleResult();
|
||||||
.getSingleResult();
|
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
|
|
||||||
QRecipe recipe = new QRecipe("alias");
|
QRecipe recipe = new QRecipe("alias");
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail)
|
.leftJoin(QCocktail.cocktail.recipe, recipe)
|
||||||
.leftJoin(QCocktail.cocktail.recipe, recipe)
|
.where(recipe.isNull())
|
||||||
.where(recipe.isNull()).fetchOne();
|
.fetchOne();
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
|
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail)
|
.leftJoin(QRecipe.recipe)
|
||||||
.leftJoin(QRecipe.recipe)
|
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
|
||||||
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
|
.where(QRecipe.recipe.isNull())
|
||||||
.where(QRecipe.recipe.isNull()).fetchOne();
|
.fetchOne();
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenQueringForCocktailThatHasRecipes_thenTheExpectedCocktailReturned() {
|
public void whenQueringForCocktailThatHasRecipes_thenTheExpectedCocktailReturned() {
|
||||||
// JPQL
|
// JPQL
|
||||||
Cocktail cocktail = entityManager
|
Cocktail cocktail = entityManager.createQuery("select c "
|
||||||
.createQuery("select c from Cocktail c join c.recipeList", Cocktail.class)
|
+ "from Cocktail c join c.recipeList", Cocktail.class)
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
|
|
||||||
cocktail = entityManager
|
cocktail = entityManager.createQuery("select c "
|
||||||
.createQuery("select c from Cocktail c join MultipleRecipe mr on mr.cocktail = c.name", Cocktail.class)
|
+ "from Cocktail c join MultipleRecipe mr "
|
||||||
.getSingleResult();
|
+ "on mr.cocktail = c.name", Cocktail.class)
|
||||||
|
.getSingleResult();
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
|
|
||||||
// QueryDSL
|
// QueryDSL
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail).join(QCocktail.cocktail.recipeList).fetchOne();
|
.join(QCocktail.cocktail.recipeList)
|
||||||
|
.fetchOne();
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
|
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.join(QMultipleRecipe.multipleRecipe)
|
.join(QMultipleRecipe.multipleRecipe)
|
||||||
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
|
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
|
||||||
.fetchOne();
|
.fetchOne();
|
||||||
verifyResult(mojito, cocktail);
|
verifyResult(mojito, cocktail);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenQueryingForCocktailThatHasNotRecipes_thenTheExpectedCocktailReturned() {
|
public void whenQueryingForCocktailThatHasNotRecipes_thenTheExpectedCocktailReturned() {
|
||||||
// JPQL
|
// JPQL
|
||||||
Cocktail cocktail = entityManager
|
Cocktail cocktail = entityManager.createQuery("select c "
|
||||||
.createQuery("select c from Cocktail c left join c.recipeList r where r is null", Cocktail.class)
|
+ "from Cocktail c left join c.recipeList r "
|
||||||
.getSingleResult();
|
+ "where r is null", Cocktail.class)
|
||||||
|
.getSingleResult();
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
|
|
||||||
cocktail = entityManager.createQuery("select c from Cocktail c left join MultipleRecipe r " +
|
cocktail = entityManager.createQuery("select c "
|
||||||
"on c.name = r.cocktail where r is null", Cocktail.class)
|
+ "from Cocktail c left join MultipleRecipe r "
|
||||||
.getSingleResult();
|
+ "on c.name = r.cocktail "
|
||||||
|
+ "where r is null", Cocktail.class)
|
||||||
|
.getSingleResult();
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
|
|
||||||
// QueryDSL
|
// QueryDSL
|
||||||
QMultipleRecipe multipleRecipe = new QMultipleRecipe("alias");
|
QMultipleRecipe multipleRecipe = new QMultipleRecipe("alias");
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail)
|
.leftJoin(QCocktail.cocktail.recipeList, multipleRecipe)
|
||||||
.leftJoin(QCocktail.cocktail.recipeList, multipleRecipe)
|
.where(multipleRecipe.isNull())
|
||||||
.where(multipleRecipe.isNull())
|
.fetchOne();
|
||||||
.fetchOne();
|
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
|
|
||||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||||
.from(QCocktail.cocktail).leftJoin(QMultipleRecipe.multipleRecipe)
|
.leftJoin(QMultipleRecipe.multipleRecipe)
|
||||||
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
|
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
|
||||||
.where(QMultipleRecipe.multipleRecipe.isNull()).fetchOne();
|
.where(QMultipleRecipe.multipleRecipe.isNull())
|
||||||
|
.fetchOne();
|
||||||
verifyResult(ginTonic, cocktail);
|
verifyResult(ginTonic, cocktail);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,25 +162,23 @@ public class UnrelatedEntitiesUnitTest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// JPQL
|
// JPQL
|
||||||
List<MultipleRecipe> recipes = entityManager
|
List<MultipleRecipe> recipes = entityManager.createQuery("select distinct r "
|
||||||
.createQuery("select distinct r from MultipleRecipe r join Cocktail c " +
|
+ "from MultipleRecipe r "
|
||||||
"on r.cocktail = c.name " +
|
+ "join Cocktail c "
|
||||||
"and " +
|
+ "on r.cocktail = c.name and r.baseIngredient = :category",
|
||||||
"r.baseIngredient = :category",
|
MultipleRecipe.class)
|
||||||
MultipleRecipe.class)
|
.setParameter("category", mojito.getCategory())
|
||||||
.setParameter("category", mojito.getCategory())
|
.getResultList();
|
||||||
.getResultList();
|
|
||||||
verifyResult.accept(recipes);
|
verifyResult.accept(recipes);
|
||||||
|
|
||||||
// QueryDSL
|
// QueryDSL
|
||||||
QCocktail cocktail = QCocktail.cocktail;
|
QCocktail cocktail = QCocktail.cocktail;
|
||||||
QMultipleRecipe multipleRecipe = QMultipleRecipe.multipleRecipe;
|
QMultipleRecipe multipleRecipe = QMultipleRecipe.multipleRecipe;
|
||||||
recipes = new JPAQuery<MultipleRecipe>(entityManager)
|
recipes = new JPAQuery<MultipleRecipe>(entityManager).from(multipleRecipe)
|
||||||
.from(multipleRecipe)
|
.join(cocktail)
|
||||||
.join(cocktail)
|
.on(multipleRecipe.cocktail.eq(cocktail.name)
|
||||||
.on(multipleRecipe.cocktail.eq(cocktail.name)
|
.and(multipleRecipe.baseIngredient.eq(mojito.getCategory())))
|
||||||
.and(multipleRecipe.baseIngredient.eq(mojito.getCategory())))
|
.fetch();
|
||||||
.fetch();
|
|
||||||
verifyResult.accept(recipes);
|
verifyResult.accept(recipes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user