From 1a7476c4f400c9925273ef5e041d8cb4e7418e36 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sat, 24 Dec 2016 03:15:53 +0100 Subject: [PATCH] Fixing OneToMany and ManyToOne annotations Also making work auditory for AdDescription :) --- .../java/de/spring/example/persistence/domain/Ad.java | 17 +++++------------ .../example/persistence/domain/AdDescription.java | 7 +++---- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/Ad.java b/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/Ad.java index dbfb184..6cc271b 100644 --- a/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/Ad.java +++ b/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/Ad.java @@ -12,7 +12,6 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.JoinColumn; //import javax.persistence.NamedNativeQueries; //import javax.persistence.NamedNativeQuery; //import javax.persistence.NamedQueries; @@ -29,12 +28,13 @@ import org.hibernate.envers.Audited; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import de.spring.example.persistence.converters.OffsetDateTimeAttributeConverter; @Entity -//@Audited(withModifiedFlag=true) +@Audited(withModifiedFlag=true) @Table(name="AD", schema="mybatis_example") @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="jsonId") // 1. Named query is JPL. It is portable. @@ -68,42 +68,35 @@ import de.spring.example.persistence.converters.OffsetDateTimeAttributeConverter //) public class Ad implements Serializable { - @Audited(withModifiedFlag=true) @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ID", updatable=false, nullable=false) private Long id; - // NO WAY OF AUDITING THIS COLUMN :(. AM I MESSING SOMETHING UP WITH JPA AND RELATIONS BETWEEN TABLES? @AuditJoinTable(name="AD_DESCRIPTION_AUDITED") - @AuditMappedBy(mappedBy="ad", positionMappedBy="id") - @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL) - @JoinColumn(name = "AD_ID", nullable = false) + @AuditMappedBy(mappedBy="ad") + @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="ad") + @JsonManagedReference private Set adDescriptions; - @Audited(withModifiedFlag=true) @Max(60) @Column(name="COMPANY_ID") private Long companyId; - @Audited(withModifiedFlag=true) @Max(40) @Column(name="COMPANY_CATEG_ID") private Long companyCategId; - @Audited(withModifiedFlag=true) @Size(min=2, max=255) @Column(name="AD_MOBILE_IMAGE") private String adMobileImage; - @Audited(withModifiedFlag=true) @NotNull @Convert(converter=OffsetDateTimeAttributeConverter.class) @Column(name="CREATED_AT", nullable=false) @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ") private OffsetDateTime createdAt; - @Audited(withModifiedFlag=true) @NotNull @Convert(converter=OffsetDateTimeAttributeConverter.class) @Column(name="UPDATED_AT", nullable = false) diff --git a/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/AdDescription.java b/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/AdDescription.java index 0b07605..856b3ac 100644 --- a/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/AdDescription.java +++ b/SpringJava/Gradle/spring-jpa-persistence/src/main/java/de/spring/example/persistence/domain/AdDescription.java @@ -2,14 +2,12 @@ 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; -import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.validation.constraints.Max; @@ -18,6 +16,7 @@ import javax.validation.constraints.Size; import org.hibernate.envers.Audited; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @@ -32,8 +31,8 @@ public class AdDescription implements Serializable { @Column(name="ID", updatable=false, nullable=false) private Long id; - @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL, optional=true) - @JoinColumn(name="AD_ID", nullable=false, updatable = false, insertable = false, referencedColumnName="ID") + @ManyToOne(fetch=FetchType.LAZY, optional=false) + @JsonBackReference private Ad ad; @NotNull -- 2.1.4