Spring JPA: what a mess...
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 22:05:09 +0000 (00:05 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 22:05:09 +0000 (00:05 +0200)
SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/Ad.java
SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml
SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml

index 4b601fe..df2046f 100644 (file)
@@ -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<AdDescription> adDescriptions;
        
        @Max(60)
index a74056a..a80d040 100644 (file)
@@ -16,9 +16,7 @@
        <context:annotation-config />\r
 \r
     <context:component-scan base-package="de.spring.example.persistence" />\r
-    \r
-    <context:property-placeholder location="classpath:jpa.properties" />\r
-    \r
+        \r
        <bean id="entityManagerFactory"\r
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">\r
                <property name="dataSource" ref="dataSource" />\r
index 94eab34..ae40307 100644 (file)
@@ -10,4 +10,6 @@
         
        <context:component-scan base-package="de.spring.example.services"/>
        
+       <context:property-placeholder location="classpath:jpa.properties" />
+       
 </beans>
\ No newline at end of file