From: Gustavo Martin Morcuende Date: Sun, 3 Jul 2016 22:05:09 +0000 (+0200) Subject: Spring JPA: what a mess... X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=89e6b6512c0f74dedb3c45ea68bf645caf61b830;p=JavaForFun Spring JPA: what a mess... --- 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