Spring JPA: trying to make work inserts with Ad+AdDescription
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 21:03:53 +0000 (23:03 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 21:03:53 +0000 (23:03 +0200)
SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/Ad.java
SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/AdDescription.java
SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java

index 5ef5871..4b601fe 100644 (file)
@@ -4,6 +4,7 @@ import java.io.Serializable;
 import java.time.OffsetDateTime;
 import java.util.Set;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Convert;
 import javax.persistence.Entity;
@@ -63,7 +64,8 @@ public class Ad implements Serializable {
        @Column(name="id", updatable=false, nullable=false)
        private Long id;
        
-       @OneToMany(mappedBy="ad", fetch=FetchType.LAZY, targetEntity=AdDescription.class)
+       @OneToMany(mappedBy="ad", fetch=FetchType.LAZY,
+                       cascade = CascadeType.ALL, targetEntity=AdDescription.class)
        private Set<AdDescription> adDescriptions;
        
        @Max(60)
@@ -96,9 +98,11 @@ public class Ad implements Serializable {
        }
 
        // It will be used by my code (for example by Unit Tests)
-       public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, OffsetDateTime createdAt,
-                       OffsetDateTime updatedAt) {
+       public Ad(Long id, Set<AdDescription> adDescriptions, Long companyId, Long companyCategId, String adMobileImage,
+                       OffsetDateTime createdAt, OffsetDateTime updatedAt) {
                this.id = id;
+               this.adDescriptions = adDescriptions;
+               this.companyId = companyId;
                this.companyCategId = companyCategId;
                this.adMobileImage = adMobileImage;
                this.createdAt = createdAt;
@@ -109,6 +113,10 @@ public class Ad implements Serializable {
                return id;
        }
 
+       public Set<AdDescription> getAdDescriptions() {
+               return adDescriptions;
+       }
+
        public Long getCompanyId() {
                return companyId;
        }
index 9a65407..a0fde9b 100644 (file)
@@ -2,8 +2,10 @@ package de.spring.example.persistence.domain;
 
 import java.io.Serializable;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
@@ -14,8 +16,6 @@ import javax.validation.constraints.Max;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
 
-import de.spring.example.persistence.domain.Ad;
-
 @Entity
 @Table(name="ad_description", schema="mybatis_example")
 public class AdDescription implements Serializable {
@@ -25,14 +25,13 @@ public class AdDescription implements Serializable {
        @Column(name="id", updatable=false, nullable=false)
        private Long id;
        
-       @NotNull
-       @ManyToOne(optional=false)
+       @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL, optional=false)
        @JoinColumn(name="ad_id", referencedColumnName="id")
        private Ad ad;
        
        @NotNull
        @Max(60)
-       @Column(name="language_id")
+       @Column(name="laguage_id")
        private Long languageId;
        
        @NotNull
@@ -64,8 +63,8 @@ public class AdDescription implements Serializable {
        public AdDescription(Long id, Ad ad, Long languageId, String adName, String adDescription,
                        String adMobileText, String adLink) {
                this.id = id;
-               this.languageId = languageId;
                this.ad = ad;
+               this.languageId = languageId;
                this.adName = adName;
                this.adDescription = adDescription;
                this.adMobileText = adMobileText;
index 183ae4d..49918ad 100644 (file)
@@ -16,6 +16,6 @@ import de.spring.example.persistence.domain.AdDescription;
  */
 public interface AdDescriptionRepository extends PagingAndSortingRepository<AdDescription, Long> {
 
-       // Custom Query method
+       // Custom Query method (useful when the offered methods by PagingAndSortingRepository are not enough)
        Page<AdDescription> findByAd(Ad ad, Pageable pageable);
 }