[BAEL-3936] Formatted code
This commit is contained in:
parent
5a56276893
commit
9acc69220b
@ -24,18 +24,21 @@ public class Cocktail {
|
||||
@NotFound(action = NotFoundAction.IGNORE)
|
||||
@JoinColumn(name = "cocktail_name",
|
||||
referencedColumnName = "cocktail",
|
||||
insertable = false,
|
||||
updatable = false,
|
||||
foreignKey = @javax.persistence.ForeignKey(value= ConstraintMode.NO_CONSTRAINT))
|
||||
insertable = false, updatable = false,
|
||||
foreignKey = @javax.persistence
|
||||
.ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
|
||||
)
|
||||
private Recipe recipe;
|
||||
|
||||
@OneToMany
|
||||
@NotFound(action = NotFoundAction.IGNORE)
|
||||
@JoinColumn(name = "cocktail",
|
||||
@JoinColumn(
|
||||
name = "cocktail",
|
||||
referencedColumnName = "cocktail_name",
|
||||
insertable = false,
|
||||
updatable = false,
|
||||
foreignKey = @javax.persistence.ForeignKey(value= ConstraintMode.NO_CONSTRAINT))
|
||||
foreignKey = @javax.persistence
|
||||
.ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
|
||||
private List<MultipleRecipe> recipeList;
|
||||
|
||||
public Cocktail() {
|
||||
@ -69,8 +72,10 @@ public class Cocktail {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Cocktail cocktail = (Cocktail) o;
|
||||
return Double.compare(cocktail.price, price) == 0 &&
|
||||
Objects.equals(name, cocktail.name) &&
|
||||
|
@ -22,13 +22,15 @@ public class MultipleRecipe {
|
||||
@Column(name = "base_ingredient")
|
||||
private String baseIngredient;
|
||||
|
||||
public MultipleRecipe() {}
|
||||
public MultipleRecipe() {
|
||||
}
|
||||
|
||||
public MultipleRecipe(Long id, String cocktail, String instructions, String baseIngredient) {
|
||||
this.baseIngredient = baseIngredient;
|
||||
this.cocktail = cocktail;
|
||||
public MultipleRecipe(Long id, String cocktail,
|
||||
String instructions, String baseIngredient) {
|
||||
this.id = id;
|
||||
this.cocktail = cocktail;
|
||||
this.instructions = instructions;
|
||||
this.baseIngredient = baseIngredient;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@ -49,8 +51,10 @@ public class MultipleRecipe {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
MultipleRecipe that = (MultipleRecipe) o;
|
||||
return Objects.equals(id, that.id) &&
|
||||
Objects.equals(cocktail, that.cocktail) &&
|
||||
@ -60,6 +64,7 @@ public class MultipleRecipe {
|
||||
|
||||
@Override
|
||||
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(ginTonic);
|
||||
entityManager.persist(new Recipe(mojito.getName(), "Some instructions"));
|
||||
entityManager.persist(new MultipleRecipe(1L, mojito.getName(), "some instructions", mojito.getCategory()));
|
||||
entityManager.persist(new MultipleRecipe(2L, mojito.getName(), "some other instructions", mojito.getCategory()));
|
||||
entityManager.persist(new MultipleRecipe(1L, mojito.getName(),
|
||||
"some instructions", mojito.getCategory()));
|
||||
entityManager.persist(new MultipleRecipe(2L, mojito.getName(),
|
||||
"some other instructions", mojito.getCategory()));
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
@ -39,25 +41,24 @@ public class UnrelatedEntitiesUnitTest {
|
||||
@Test
|
||||
public void whenQueryingForCocktailThatHasRecipe_thenTheExpectedCocktailReturned() {
|
||||
// JPA
|
||||
Cocktail cocktail = entityManager.createQuery(
|
||||
"select c from Cocktail c join c.recipe", Cocktail.class)
|
||||
Cocktail cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c join c.recipe", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(mojito, cocktail);
|
||||
|
||||
cocktail = entityManager.createQuery(
|
||||
"select c from Cocktail c join Recipe r on c.name = r.cocktail", Cocktail.class)
|
||||
cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c join Recipe r "
|
||||
+ "on c.name = r.cocktail", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(mojito, cocktail);
|
||||
|
||||
// QueryDSL
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail)
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.join(QCocktail.cocktail.recipe)
|
||||
.fetchOne();
|
||||
verifyResult(mojito, cocktail);
|
||||
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail)
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.join(QRecipe.recipe)
|
||||
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
|
||||
.fetchOne();
|
||||
@ -66,52 +67,52 @@ public class UnrelatedEntitiesUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenQueryingForCocktailThatHasNotARecipe_thenTheExpectedCocktailReturned() {
|
||||
Cocktail cocktail = entityManager
|
||||
.createQuery("select c from Cocktail c left join c.recipe r " +
|
||||
"where r is null",
|
||||
Cocktail.class)
|
||||
Cocktail cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c left join c.recipe r "
|
||||
+ "where r is null", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
|
||||
cocktail = entityManager
|
||||
.createQuery("select c from Cocktail c left join Recipe r " +
|
||||
"on c.name = r.cocktail " +
|
||||
"where r is null",
|
||||
Cocktail.class)
|
||||
cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c left join Recipe r "
|
||||
+ "on c.name = r.cocktail "
|
||||
+ "where r is null", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
|
||||
QRecipe recipe = new QRecipe("alias");
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail)
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.leftJoin(QCocktail.cocktail.recipe, recipe)
|
||||
.where(recipe.isNull()).fetchOne();
|
||||
.where(recipe.isNull())
|
||||
.fetchOne();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail)
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.leftJoin(QRecipe.recipe)
|
||||
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
|
||||
.where(QRecipe.recipe.isNull()).fetchOne();
|
||||
.where(QRecipe.recipe.isNull())
|
||||
.fetchOne();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQueringForCocktailThatHasRecipes_thenTheExpectedCocktailReturned() {
|
||||
// JPQL
|
||||
Cocktail cocktail = entityManager
|
||||
.createQuery("select c from Cocktail c join c.recipeList", Cocktail.class)
|
||||
Cocktail cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c join c.recipeList", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(mojito, cocktail);
|
||||
|
||||
cocktail = entityManager
|
||||
.createQuery("select c from Cocktail c join MultipleRecipe mr on mr.cocktail = c.name", Cocktail.class)
|
||||
cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c join MultipleRecipe mr "
|
||||
+ "on mr.cocktail = c.name", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(mojito, cocktail);
|
||||
|
||||
// QueryDSL
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail).join(QCocktail.cocktail.recipeList).fetchOne();
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.join(QCocktail.cocktail.recipeList)
|
||||
.fetchOne();
|
||||
verifyResult(mojito, cocktail);
|
||||
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
@ -124,29 +125,32 @@ public class UnrelatedEntitiesUnitTest {
|
||||
@Test
|
||||
public void whenQueryingForCocktailThatHasNotRecipes_thenTheExpectedCocktailReturned() {
|
||||
// JPQL
|
||||
Cocktail cocktail = entityManager
|
||||
.createQuery("select c from Cocktail c left join c.recipeList r where r is null", Cocktail.class)
|
||||
Cocktail cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c left join c.recipeList r "
|
||||
+ "where r is null", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
|
||||
cocktail = entityManager.createQuery("select c from Cocktail c left join MultipleRecipe r " +
|
||||
"on c.name = r.cocktail where r is null", Cocktail.class)
|
||||
cocktail = entityManager.createQuery("select c "
|
||||
+ "from Cocktail c left join MultipleRecipe r "
|
||||
+ "on c.name = r.cocktail "
|
||||
+ "where r is null", Cocktail.class)
|
||||
.getSingleResult();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
|
||||
// QueryDSL
|
||||
QMultipleRecipe multipleRecipe = new QMultipleRecipe("alias");
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail)
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.leftJoin(QCocktail.cocktail.recipeList, multipleRecipe)
|
||||
.where(multipleRecipe.isNull())
|
||||
.fetchOne();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager)
|
||||
.from(QCocktail.cocktail).leftJoin(QMultipleRecipe.multipleRecipe)
|
||||
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
|
||||
.leftJoin(QMultipleRecipe.multipleRecipe)
|
||||
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
|
||||
.where(QMultipleRecipe.multipleRecipe.isNull()).fetchOne();
|
||||
.where(QMultipleRecipe.multipleRecipe.isNull())
|
||||
.fetchOne();
|
||||
verifyResult(ginTonic, cocktail);
|
||||
}
|
||||
|
||||
@ -158,11 +162,10 @@ public class UnrelatedEntitiesUnitTest {
|
||||
};
|
||||
|
||||
// JPQL
|
||||
List<MultipleRecipe> recipes = entityManager
|
||||
.createQuery("select distinct r from MultipleRecipe r join Cocktail c " +
|
||||
"on r.cocktail = c.name " +
|
||||
"and " +
|
||||
"r.baseIngredient = :category",
|
||||
List<MultipleRecipe> recipes = entityManager.createQuery("select distinct r "
|
||||
+ "from MultipleRecipe r "
|
||||
+ "join Cocktail c "
|
||||
+ "on r.cocktail = c.name and r.baseIngredient = :category",
|
||||
MultipleRecipe.class)
|
||||
.setParameter("category", mojito.getCategory())
|
||||
.getResultList();
|
||||
@ -171,8 +174,7 @@ public class UnrelatedEntitiesUnitTest {
|
||||
// QueryDSL
|
||||
QCocktail cocktail = QCocktail.cocktail;
|
||||
QMultipleRecipe multipleRecipe = QMultipleRecipe.multipleRecipe;
|
||||
recipes = new JPAQuery<MultipleRecipe>(entityManager)
|
||||
.from(multipleRecipe)
|
||||
recipes = new JPAQuery<MultipleRecipe>(entityManager).from(multipleRecipe)
|
||||
.join(cocktail)
|
||||
.on(multipleRecipe.cocktail.eq(cocktail.name)
|
||||
.and(multipleRecipe.baseIngredient.eq(mojito.getCategory())))
|
||||
|
Loading…
x
Reference in New Issue
Block a user