Merge pull request #8012 from fanatixan/bael-3089
BAEL-3089 - created examples for @SecondaryTable article
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.jpa.multipletables.multipleentities;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.PrimaryKeyJoinColumn;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "allergens")
|
||||
public class AllergensAsEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "meal_id")
|
||||
private Long mealId;
|
||||
|
||||
@OneToOne
|
||||
@PrimaryKeyJoinColumn(name = "meal_id")
|
||||
private MealAsSingleEntity meal;
|
||||
|
||||
@Column(name = "peanuts")
|
||||
private boolean peanuts;
|
||||
|
||||
@Column(name = "celery")
|
||||
private boolean celery;
|
||||
|
||||
@Column(name = "sesame_seeds")
|
||||
private boolean sesameSeeds;
|
||||
|
||||
public MealAsSingleEntity getMeal() {
|
||||
return meal;
|
||||
}
|
||||
|
||||
public void setMeal(MealAsSingleEntity meal) {
|
||||
this.meal = meal;
|
||||
}
|
||||
|
||||
public boolean isPeanuts() {
|
||||
return peanuts;
|
||||
}
|
||||
|
||||
public void setPeanuts(boolean peanuts) {
|
||||
this.peanuts = peanuts;
|
||||
}
|
||||
|
||||
public boolean isCelery() {
|
||||
return celery;
|
||||
}
|
||||
|
||||
public void setCelery(boolean celery) {
|
||||
this.celery = celery;
|
||||
}
|
||||
|
||||
public boolean isSesameSeeds() {
|
||||
return sesameSeeds;
|
||||
}
|
||||
|
||||
public void setSesameSeeds(boolean sesameSeeds) {
|
||||
this.sesameSeeds = sesameSeeds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AllergensAsEntity [peanuts=" + peanuts + ", celery=" + celery + ", sesameSeeds=" + sesameSeeds + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.jpa.multipletables.multipleentities;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "meal")
|
||||
public class MealWithMultipleEntities {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@Column(name = "price")
|
||||
private BigDecimal price;
|
||||
|
||||
@OneToOne(mappedBy = "meal")
|
||||
private AllergensAsEntity allergens;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public AllergensAsEntity getAllergens() {
|
||||
return allergens;
|
||||
}
|
||||
|
||||
public void setAllergens(AllergensAsEntity allergens) {
|
||||
this.allergens = allergens;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MealWithMultipleEntities [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + ", allergens=" + allergens + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.baeldung.jpa.multipletables.secondarytable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.PrimaryKeyJoinColumn;
|
||||
import javax.persistence.SecondaryTable;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "meal")
|
||||
@SecondaryTable(name = "allergens", pkJoinColumns = @PrimaryKeyJoinColumn(name = "meal_id"))
|
||||
public class MealAsSingleEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@Column(name = "price")
|
||||
private BigDecimal price;
|
||||
|
||||
@Column(name = "peanuts", table = "allergens")
|
||||
private boolean peanuts;
|
||||
|
||||
@Column(name = "celery", table = "allergens")
|
||||
private boolean celery;
|
||||
|
||||
@Column(name = "sesame_seeds", table = "allergens")
|
||||
private boolean sesameSeeds;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public boolean isPeanuts() {
|
||||
return peanuts;
|
||||
}
|
||||
|
||||
public void setPeanuts(boolean peanuts) {
|
||||
this.peanuts = peanuts;
|
||||
}
|
||||
|
||||
public boolean isCelery() {
|
||||
return celery;
|
||||
}
|
||||
|
||||
public void setCelery(boolean celery) {
|
||||
this.celery = celery;
|
||||
}
|
||||
|
||||
public boolean isSesameSeeds() {
|
||||
return sesameSeeds;
|
||||
}
|
||||
|
||||
public void setSesameSeeds(boolean sesameSeeds) {
|
||||
this.sesameSeeds = sesameSeeds;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MealAsSingleEntity [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + ", peanuts=" + peanuts + ", celery=" + celery + ", sesameSeeds=" + sesameSeeds + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.jpa.multipletables.secondarytable.embeddable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class AllergensAsEmbeddable {
|
||||
|
||||
@Column(name = "peanuts", table = "allergens")
|
||||
private boolean peanuts;
|
||||
|
||||
@Column(name = "celery", table = "allergens")
|
||||
private boolean celery;
|
||||
|
||||
@Column(name = "sesame_seeds", table = "allergens")
|
||||
private boolean sesameSeeds;
|
||||
|
||||
public boolean isPeanuts() {
|
||||
return peanuts;
|
||||
}
|
||||
|
||||
public void setPeanuts(boolean peanuts) {
|
||||
this.peanuts = peanuts;
|
||||
}
|
||||
|
||||
public boolean isCelery() {
|
||||
return celery;
|
||||
}
|
||||
|
||||
public void setCelery(boolean celery) {
|
||||
this.celery = celery;
|
||||
}
|
||||
|
||||
public boolean isSesameSeeds() {
|
||||
return sesameSeeds;
|
||||
}
|
||||
|
||||
public void setSesameSeeds(boolean sesameSeeds) {
|
||||
this.sesameSeeds = sesameSeeds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AllergensAsEmbeddable [peanuts=" + peanuts + ", celery=" + celery + ", sesameSeeds=" + sesameSeeds + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.jpa.multipletables.secondarytable.embeddable;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embedded;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.PrimaryKeyJoinColumn;
|
||||
import javax.persistence.SecondaryTable;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "meal")
|
||||
@SecondaryTable(name = "allergens", pkJoinColumns = @PrimaryKeyJoinColumn(name = "meal_id"))
|
||||
public class MealWithEmbeddedAllergens {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@Column(name = "price")
|
||||
private BigDecimal price;
|
||||
|
||||
@Embedded
|
||||
private AllergensAsEmbeddable allergens;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public AllergensAsEmbeddable getAllergens() {
|
||||
return allergens;
|
||||
}
|
||||
|
||||
public void setAllergens(AllergensAsEmbeddable allergens) {
|
||||
this.allergens = allergens;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MealWithEmbeddedAllergens [id=" + id + ", name=" + name + ", description=" + description + ", price=" + price + ", allergens=" + allergens + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -135,4 +135,32 @@
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
<persistence-unit name="jpa-h2-multipltables">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.multipletables.multipleentities.MealWithMultipleEntities</class>
|
||||
<class>com.baeldung.jpa.multipletables.multipleentities.AllergensAsEntity</class>
|
||||
|
||||
<class>com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity</class>
|
||||
|
||||
<class>com.baeldung.jpa.multipletables.secondarytable.embeddable.MealWithEmbeddedAllergens</class>
|
||||
<class>com.baeldung.jpa.multipletables.secondarytable.embeddable.AllergensAsEmbeddable</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="hibernate.hbm2ddl.import_files" value="multipletables.sql" />
|
||||
<property name="show_sql" value="true" />
|
||||
<property name="hibernate.temp.use_jdbc_metadata_defaults"
|
||||
value="false" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
|
||||
</persistence>
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.jpa.multipletables;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jpa.multipletables.multipleentities.MealWithMultipleEntities;
|
||||
import com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity;
|
||||
import com.baeldung.jpa.multipletables.secondarytable.embeddable.MealWithEmbeddedAllergens;
|
||||
|
||||
public class MultipleTablesIntegrationTest {
|
||||
|
||||
private static EntityManagerFactory emf;
|
||||
private static EntityManager em;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
emf = Persistence.createEntityManagerFactory("jpa-h2-multipltables");
|
||||
em = emf.createEntityManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void entityManager_shouldLoadMealAsSingleEntity() {
|
||||
// given
|
||||
|
||||
// when
|
||||
MealAsSingleEntity meal = em.find(MealAsSingleEntity.class, 1L);
|
||||
|
||||
// then
|
||||
assertThat(meal).isNotNull();
|
||||
assertThat(meal.getId()).isEqualTo(1L);
|
||||
assertThat(meal.isPeanuts()).isFalse();
|
||||
assertThat(meal.isCelery()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void entityManager_shouldLoadMealWithEmbeddedAllergens() {
|
||||
// given
|
||||
|
||||
// when
|
||||
MealWithEmbeddedAllergens meal = em.find(MealWithEmbeddedAllergens.class, 1L);
|
||||
|
||||
// then
|
||||
assertThat(meal).isNotNull();
|
||||
assertThat(meal.getId()).isEqualTo(1L);
|
||||
assertThat(meal.getAllergens()).isNotNull();
|
||||
assertThat(meal.getAllergens().isPeanuts()).isFalse();
|
||||
assertThat(meal.getAllergens().isCelery()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void entityManager_shouldLoadMealWithAllergensEntity() {
|
||||
// given
|
||||
|
||||
// when
|
||||
MealWithMultipleEntities meal = em.find(MealWithMultipleEntities.class, 1L);
|
||||
|
||||
// then
|
||||
assertThat(meal).isNotNull();
|
||||
assertThat(meal.getId()).isEqualTo(1L);
|
||||
assertThat(meal.getAllergens()).isNotNull();
|
||||
assertThat(meal.getAllergens().isPeanuts()).isFalse();
|
||||
assertThat(meal.getAllergens().isCelery()).isTrue();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void teardown() {
|
||||
if (emf != null) {
|
||||
emf.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
drop table if exists allergens;
|
||||
drop table if exists meal;
|
||||
|
||||
create table meal (id bigint auto_increment, name varchar(255) not null, description varchar(255) not null, price decimal(19, 2) not null, primary key (id));
|
||||
create table allergens (meal_id bigint auto_increment, peanuts number(1) not null, celery number(1) not null, sesame_seeds number(1) not null, primary key (meal_id));
|
||||
|
||||
insert into meal (id, name, description, price) values (1, 'Pizza', 'Delicious', 5);
|
||||
insert into allergens (meal_id, peanuts, celery, sesame_seeds) values (1, 0, 1, 0);
|
||||
Reference in New Issue
Block a user