[BAEL-3936] Initial commit

This commit is contained in:
kkaravitis
2020-04-05 00:45:15 +03:00
parent a1aa14b99a
commit 5a56276893
6 changed files with 437 additions and 3 deletions
@@ -0,0 +1,84 @@
package com.baeldung.jpa.unrelated.entities;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import javax.persistence.*;
import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "cocktails")
public class Cocktail {
@Id
@Column(name="cocktail_name")
private String name;
@Column
private double price;
@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))
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))
private List<MultipleRecipe> recipeList;
public Cocktail() {
}
public Cocktail(String name, double price, String baseIngredient) {
this.name = name;
this.price = price;
this.category = baseIngredient;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getCategory() {
return category;
}
public Recipe getRecipe() {
return recipe;
}
public List<MultipleRecipe> getRecipeList() {
return recipeList;
}
@Override
public boolean equals(Object o) {
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);
}
@Override
public int hashCode() {
return Objects.hash(name, price, category);
}
}
@@ -0,0 +1,65 @@
package com.baeldung.jpa.unrelated.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name="multiple_recipes")
public class MultipleRecipe {
@Id
@Column(name="id")
private Long id;
@Column(name="cocktail")
private String cocktail;
@Column(name="instructions")
private String instructions;
@Column(name="base_ingredient")
private String baseIngredient;
public MultipleRecipe() {}
public MultipleRecipe(Long id, String cocktail, String instructions, String baseIngredient) {
this.baseIngredient = baseIngredient;
this.cocktail = cocktail;
this.id = id;
this.instructions = instructions;
}
public Long getId() {
return id;
}
public String getCocktail() {
return cocktail;
}
public String getInstructions() {
return instructions;
}
public String getBaseIngredient() {
return baseIngredient;
}
@Override
public boolean equals(Object o) {
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);
}
@Override
public int hashCode() {
return Objects.hash(id, cocktail, instructions, baseIngredient);
}
}
@@ -0,0 +1,48 @@
package com.baeldung.jpa.unrelated.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Objects;
@Entity
@Table(name="recipes")
public class Recipe {
@Id
@Column(name = "cocktail")
private String cocktail;
@Column
private String instructions;
public Recipe() {
}
public Recipe(String cocktail, String instructions) {
this.cocktail = cocktail;
this.instructions = instructions;
}
public String getCocktail() {
return cocktail;
}
public String getInstructions() {
return instructions;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Recipe recipe = (Recipe) o;
return Objects.equals(cocktail, recipe.cocktail) &&
Objects.equals(instructions, recipe.instructions);
}
@Override
public int hashCode() {
return Objects.hash(cocktail, instructions);
}
}
@@ -162,5 +162,26 @@
value="false" />
</properties>
</persistence-unit>
</persistence>
<persistence-unit name="jpa-h2-unrelated-entities">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.unrelated.entities.Cocktail</class>
<class>com.baeldung.jpa.unrelated.entities.Recipe</class>
<class>com.baeldung.jpa.unrelated.entities.MultipleRecipe</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
</persistence>