Skip to content
Snippets Groups Projects
Commit 2a536d14 authored by Goik Martin's avatar Goik Martin
Browse files

@onetomany --> embedabble

parent 12cbd401
No related branches found
No related tags found
No related merge requests found
......@@ -51,12 +51,12 @@ public class RdbmsAccess {
transaction.commit();
}
private void addDescriptions(final List<Description> desccriptions, final Element productElement) {
private void addDescriptions(final List<Description> descriptions, final Element productElement) {
for(Description d: desccriptions) {
for(final Description d: descriptions) {
final Element descriptionElement = new Element("description");
productElement.addContent(descriptionElement);
descriptionElement.addContent(d.getText());
descriptionElement.addContent(d.getDescription());
}
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1.sql2catalog.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Embeddable;
@Table(
uniqueConstraints=
@UniqueConstraint(columnNames={"product", "orderIndex"})
)
@Entity
public class Description {
// Class not strictly required but allows for relational table's
// attribute name "description" in favour of "descriptions"
@Id
@GeneratedValue
Long id;
@Embeddable
public class Description {
protected Description(){}
public Description (final Product product, final int orderIndex, final String text) {
this.product = product;
this.orderIndex = orderIndex;
this.text = text;
product.descriptions.add(this);
}
@ManyToOne
@JoinColumn(name="product")
Product product;
int orderIndex;
@Column(nullable = false)
String text;
public int getOrderIndex() {
return orderIndex;
}
public void setOrderIndex(int orderIndex) {
this.orderIndex = orderIndex;
}
public String getText() {
return text;
protected Description() {}
public Description(final String description) {
this.description = description;
}
public void setText(String text) {
this.text = text;
String description;
public String getDescription() {
return description;
}
public Product getProduct() {
return product;
public void setDescription(String description) {
this.description = description;
}
}
}
\ No newline at end of file
......@@ -3,11 +3,13 @@ package de.hdm_stuttgart.mi.sda1.sql2catalog.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;
import javax.persistence.OrderColumn;
@Entity
public class Product {
......@@ -28,7 +30,9 @@ public class Product {
Integer age;
@OneToMany(mappedBy = "product", cascade=CascadeType.ALL)
@ElementCollection
@CollectionTable(name="Description", joinColumns={@JoinColumn(name="product")})
@OrderColumn(name="orderIndex")
List<Description> descriptions = new ArrayList<Description>();
public int getId() {
......
......@@ -26,13 +26,13 @@ public class TestSchema {
transaction.begin();
final Product monkeyPickedTea = new Product(1, "Monkey Picked Tea", null);
new Description(monkeyPickedTea, 0, "Picked only by specially trained monkeys");
new Description(monkeyPickedTea, 1, "Rare wild Chinese tea");
monkeyPickedTea.getDescriptions().add(new Description("Picked by specially trained monkeys"));
monkeyPickedTea.getDescriptions().add(new Description("Rare wild Chinese tea"));
final Product instantTent = new Product(2, "4-Person Instant Tent", 15);
new Description(instantTent, 0, "Exclusive WeatherTec system.");
new Description(instantTent, 1, "4-person, 1-compartment tent");
new Description(instantTent, 2, "Pre-attached tent poles");
instantTent.getDescriptions().add(new Description("Exclusive WeatherTec system."));
instantTent.getDescriptions().add(new Description("4-person, 1-compartment tent"));
instantTent.getDescriptions().add(new Description("Pre-attached tent poles"));
em.persist(monkeyPickedTea);
em.persist(instantTent);
......
package rdbms2catalog;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.junit.Test;
import de.hdm_stuttgart.mi.sda1.sql2catalog.model.Description;
import de.hdm_stuttgart.mi.sda1.sql2catalog.model.Product;
public class TestSchemaConflict {
static final EntityManager em;
static {
final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("create-drop");
em = emFactory.createEntityManager();
}
@Test(expected=javax.persistence.PersistenceException.class)
public void insertData() {
{
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
final Product monkeyPickedTea = new Product(1, "Monkey Picked Tea", null);
new Description(monkeyPickedTea, 0, "Picked only by specially trained monkeys");
new Description(monkeyPickedTea, 0, "Rare wild Chinese tea"); // oops! Unique key violation
em.persist(monkeyPickedTea);
transaction.commit();
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment