From 89e6b6512c0f74dedb3c45ea68bf645caf61b830 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 4 Jul 2016 00:05:09 +0200 Subject: [PATCH] Spring JPA: what a mess... --- .../main/java/de/spring/example/persistence/domain/Ad.java | 14 ++++++++++++-- .../resources/spring-configuration/jpa-configuration.xml | 4 +--- .../spring-configuration/spring-configuration.xml | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/Ad.java b/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/Ad.java index 4b601fe..df2046f 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/Ad.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/Ad.java @@ -12,6 +12,8 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; //import javax.persistence.NamedNativeQueries; //import javax.persistence.NamedNativeQuery; //import javax.persistence.NamedQueries; @@ -64,8 +66,16 @@ public class Ad implements Serializable { @Column(name="id", updatable=false, nullable=false) private Long id; - @OneToMany(mappedBy="ad", fetch=FetchType.LAZY, - cascade = CascadeType.ALL, targetEntity=AdDescription.class) +// 1. Using just OneToMany: +// @OneToMany(mappedBy="ad", fetch=FetchType.LAZY, +// cascade = CascadeType.ALL, targetEntity=AdDescription.class) +// 2. The same using @OneToMany + @JoinTable: + @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, targetEntity=AdDescription.class) + @JoinTable( + name="ad_description", + joinColumns = @JoinColumn( name="id"), + inverseJoinColumns = @JoinColumn( name="ad_id") + ) private Set adDescriptions; @Max(60) diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml b/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml index a74056a..a80d040 100644 --- a/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml +++ b/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml @@ -16,9 +16,7 @@ - - - + diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml b/SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml index 94eab34..ae40307 100644 --- a/SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml +++ b/SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml @@ -10,4 +10,6 @@ + + \ No newline at end of file -- 2.1.4