From 01d30265e0d76e969b5873c26475475ed000857c Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 3 Jul 2016 23:03:53 +0200 Subject: [PATCH] Spring JPA: trying to make work inserts with Ad+AdDescription --- .../main/java/de/spring/example/persistence/domain/Ad.java | 14 +++++++++++--- .../spring/example/persistence/domain/AdDescription.java | 11 +++++------ .../persistence/repository/AdDescriptionRepository.java | 2 +- 3 files changed, 17 insertions(+), 10 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 5ef5871..4b601fe 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 @@ -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 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 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 getAdDescriptions() { + return adDescriptions; + } + public Long getCompanyId() { return companyId; } diff --git a/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/AdDescription.java b/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/AdDescription.java index 9a65407..a0fde9b 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/AdDescription.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/persistence/domain/AdDescription.java @@ -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; diff --git a/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java b/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java index 183ae4d..49918ad 100644 --- a/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java +++ b/SpringJava/JPA/src/main/java/de/spring/example/persistence/repository/AdDescriptionRepository.java @@ -16,6 +16,6 @@ import de.spring.example.persistence.domain.AdDescription; */ public interface AdDescriptionRepository extends PagingAndSortingRepository { - // Custom Query method + // Custom Query method (useful when the offered methods by PagingAndSortingRepository are not enough) Page findByAd(Ad ad, Pageable pageable); } -- 2.1.4