[BAEL-3936] Formatted code

This commit is contained in:
kkaravitis 2020-04-05 01:11:49 +03:00
parent 5a56276893
commit 9acc69220b
3 changed files with 123 additions and 111 deletions

View File

@ -11,31 +11,34 @@ import java.util.Objects;
@Table(name = "cocktails")
public class Cocktail {
@Id
@Column(name="cocktail_name")
@Column(name = "cocktail_name")
private String name;
@Column
private double price;
@Column(name="category")
@Column(name = "category")
private String category;
@OneToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "cocktail_name",
referencedColumnName = "cocktail",
insertable = false,
updatable = false,
foreignKey = @javax.persistence.ForeignKey(value= ConstraintMode.NO_CONSTRAINT))
@JoinColumn(name = "cocktail_name",
referencedColumnName = "cocktail",
insertable = false, updatable = false,
foreignKey = @javax.persistence
.ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
)
private Recipe recipe;
@OneToMany
@NotFound(action=NotFoundAction.IGNORE)
@JoinColumn(name = "cocktail",
referencedColumnName = "cocktail_name",
insertable = false,
updatable = false,
foreignKey = @javax.persistence.ForeignKey(value= ConstraintMode.NO_CONSTRAINT))
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(
name = "cocktail",
referencedColumnName = "cocktail_name",
insertable = false,
updatable = false,
foreignKey = @javax.persistence
.ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private List<MultipleRecipe> recipeList;
public Cocktail() {
@ -69,12 +72,14 @@ 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) &&
Objects.equals(category, cocktail.category);
return Double.compare(cocktail.price, price) == 0 &&
Objects.equals(name, cocktail.name) &&
Objects.equals(category, cocktail.category);
}
@Override

View File

@ -7,28 +7,30 @@ import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name="multiple_recipes")
@Table(name = "multiple_recipes")
public class MultipleRecipe {
@Id
@Column(name="id")
@Column(name = "id")
private Long id;
@Column(name="cocktail")
@Column(name = "cocktail")
private String cocktail;
@Column(name="instructions")
@Column(name = "instructions")
private String instructions;
@Column(name="base_ingredient")
@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,17 +51,20 @@ 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) &&
Objects.equals(instructions, that.instructions) &&
Objects.equals(baseIngredient, that.baseIngredient);
return Objects.equals(id, that.id) &&
Objects.equals(cocktail, that.cocktail) &&
Objects.equals(instructions, that.instructions) &&
Objects.equals(baseIngredient, that.baseIngredient);
}
@Override
public int hashCode() {
return Objects.hash(id, cocktail, instructions, baseIngredient);
return Objects.hash(id, cocktail,
instructions, baseIngredient);
}
}

View File

@ -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,114 +41,116 @@ public class UnrelatedEntitiesUnitTest {
@Test
public void whenQueryingForCocktailThatHasRecipe_thenTheExpectedCocktailReturned() {
// JPA
Cocktail cocktail = entityManager.createQuery(
"select c from Cocktail c join c.recipe", Cocktail.class)
.getSingleResult();
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)
.getSingleResult();
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)
.join(QCocktail.cocktail.recipe)
.fetchOne();
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
.join(QCocktail.cocktail.recipe)
.fetchOne();
verifyResult(mojito, cocktail);
cocktail = new JPAQuery<Cocktail>(entityManager)
.from(QCocktail.cocktail)
.join(QRecipe.recipe)
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
.fetchOne();
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
.join(QRecipe.recipe)
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
.fetchOne();
verifyResult(mojito, cocktail);
}
@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)
.getSingleResult();
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)
.getSingleResult();
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)
.leftJoin(QCocktail.cocktail.recipe, recipe)
.where(recipe.isNull()).fetchOne();
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
.leftJoin(QCocktail.cocktail.recipe, recipe)
.where(recipe.isNull())
.fetchOne();
verifyResult(ginTonic, 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();
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
.leftJoin(QRecipe.recipe)
.on(QCocktail.cocktail.name.eq(QRecipe.recipe.cocktail))
.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)
.getSingleResult();
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)
.getSingleResult();
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)
.join(QMultipleRecipe.multipleRecipe)
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
.fetchOne();
.join(QMultipleRecipe.multipleRecipe)
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
.fetchOne();
verifyResult(mojito, cocktail);
}
@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)
.getSingleResult();
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)
.getSingleResult();
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)
.leftJoin(QCocktail.cocktail.recipeList, multipleRecipe)
.where(multipleRecipe.isNull())
.fetchOne();
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)
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
.where(QMultipleRecipe.multipleRecipe.isNull()).fetchOne();
cocktail = new JPAQuery<Cocktail>(entityManager).from(QCocktail.cocktail)
.leftJoin(QMultipleRecipe.multipleRecipe)
.on(QCocktail.cocktail.name.eq(QMultipleRecipe.multipleRecipe.cocktail))
.where(QMultipleRecipe.multipleRecipe.isNull())
.fetchOne();
verifyResult(ginTonic, cocktail);
}
@ -158,25 +162,23 @@ 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",
MultipleRecipe.class)
.setParameter("category", mojito.getCategory())
.getResultList();
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();
verifyResult.accept(recipes);
// QueryDSL
QCocktail cocktail = QCocktail.cocktail;
QMultipleRecipe multipleRecipe = QMultipleRecipe.multipleRecipe;
recipes = new JPAQuery<MultipleRecipe>(entityManager)
.from(multipleRecipe)
.join(cocktail)
.on(multipleRecipe.cocktail.eq(cocktail.name)
.and(multipleRecipe.baseIngredient.eq(mojito.getCategory())))
.fetch();
recipes = new JPAQuery<MultipleRecipe>(entityManager).from(multipleRecipe)
.join(cocktail)
.on(multipleRecipe.cocktail.eq(cocktail.name)
.and(multipleRecipe.baseIngredient.eq(mojito.getCategory())))
.fetch();
verifyResult.accept(recipes);
}