I have three databases that I want to select based on their relative IDs.
You have the main table called sms_templates:
CREATE TABLE public.sms_templates
(
id integer NOT NULL DEFAULT nextval('sms_templates_id_seq'::regclass),
name character varying(150) COLLATE pg_catalog."default" NOT NULL,
has_variables boolean NOT NULL DEFAULT false,
default_label text COLLATE pg_catalog."default" NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
updated_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT sms_templates_pkey PRIMARY KEY (id)
)
Sms Template Langugae:
CREATE TABLE public.sms_template_languages
(
id integer NOT NULL DEFAULT nextval('sms_template_languages_id_seq'::regclass),
sms_template_id integer NOT NULL,
language_id integer NOT NULL,
label text COLLATE pg_catalog."default" NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
updated_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT sms_template_languages_pkey PRIMARY KEY (id),
CONSTRAINT sms_template_languages_language_id_fkey FOREIGN KEY (language_id)
REFERENCES public.languages (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE,
CONSTRAINT sms_template_languages_sms_template_id_fkey FOREIGN KEY (sms_template_id)
REFERENCES public.sms_templates (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
)
And finally the Languages table:
CREATE TABLE public.languages
(
id integer NOT NULL DEFAULT nextval('languages_id_seq'::regclass),
name character varying(5) COLLATE pg_catalog."default" NOT NULL,
created_at timestamp without time zone NOT NULL DEFAULT now(),
updated_at timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT languages_pkey PRIMARY KEY (id)
)
I have created their separate entities/repositories and everything works well when I join the sms_templates table with the sms_template_languages. But as soon as I join the language table so that I can get the value, everything starts crashing and I end up with the following error:
[org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Could not set field value [LanguageEntity(id=2, code=null, createdAt=null, updatedAt=null)] value by reflection : [class com.packagename.SMSTemplateLanguage.Entity.SMSTemplateLanguageEntity.languageEntitySet] setter of com.packagename.SMSTemplateLanguage.Entity.SMSTemplateLanguageEntity.languageEntitySet;
My Entities:
@Entity
@Table(name = "sms_templates")
@Data
public class SMSTemplateEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column(name="has_variables")
private Boolean hasVariables;
@Column(name = "default_label")
private String defaultLabel;
@Column(name = "created_at")
private Timestamp createdAt;
@Column(name = "updated_at")
private Timestamp updatedAt;
@OneToMany(targetEntity = SMSTemplateVariableEntity.class)
@JoinColumn(name = "sms_template_id")
private Set<SMSTemplateVariables> variableNames;
@OneToMany(targetEntity = SMSTemplateLanguageEntity.class)
@JoinColumn(name = "sms_template_id")
private Set<SMSTemplateLanguageEntity> templateLanguage;
@PrePersist
public void prePersist() {
this.createdAt = new Timestamp((System.currentTimeMillis()));
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
@PreUpdate
public void preUpdate() {
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
}
@Entity
@Table(name = "sms_template_variables")
@Data
public class SMSTemplateVariableEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "sms_template_id")
private Long smsTemplateId;
@Column(name = "variable_name")
private String variableName;
@Column(name = "created_at")
@JsonIgnore
private Timestamp createdAt;
@JsonIgnore
@Column(name = "updated_at")
private Timestamp updatedAt;
@PrePersist
public void prePersist() {
this.createdAt = new Timestamp((System.currentTimeMillis()));
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
@PreUpdate
public void preUpdate() {
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
}
@Entity
@Data
@Table(name = "sms_template_languages")
public class SMSTemplateLanguageEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "sms_template_id")
private Long smsTemplateId;
@Column
private String label;
@Column(name = "created_at")
@JsonIgnore
private Timestamp createdAt;
@Column(name = "updated_at")
@JsonIgnore
private Timestamp updatedAt;
@OneToOne(targetEntity = LanguageEntity.class)
@JoinColumn(name = "language_id")
private Set<LanguageEntity> languageEntitySet;
@PrePersist
public void prePersist() {
this.createdAt = new Timestamp((System.currentTimeMillis()));
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
@PreUpdate
public void preUpdate() {
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
}
@Data
@Entity
@Table(name = "languages")
public class LanguageEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String code;
@Column(name = "created_at")
@JsonIgnore
private Timestamp createdAt;
@JsonIgnore
@Column(name = "updated_at")
private Timestamp updatedAt;
@PrePersist
public void prePersist() {
this.createdAt = new Timestamp((System.currentTimeMillis()));
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
@PreUpdate
public void preUpdate() {
this.updatedAt = new Timestamp((System.currentTimeMillis()));
}
}
My Main repo:
@Repository
public interface SMSTemplateRepository extends CrudRepository<SMSTemplateEntity, Long> {
@Query(value = "SELECT new com.montymobile.parentalControlPortal.SMSTemplate.DTOs.SMSTemplateDTO " +
"(st) FROM SMSTemplateEntity st")
List<SMSTemplateDTO> getTemplates();
}
The DTO responsible:
@Data
public class SMSTemplateDTO {
private Long id;
private String name;
private Set<SMSTemplateVariables> variableNames;
private Set<SMSTemplateLanguageEntity> languageEntities;
public SMSTemplateDTO() {
}
public SMSTemplateDTO(SMSTemplateEntity smsTemplateEntity) {
this.id = smsTemplateEntity.getId();
this.name = smsTemplateEntity.getName();
this.variableNames = smsTemplateEntity.getVariableNames();
this.languageEntities = smsTemplateEntity.getTemplateLanguage();
}
}
Did I mix up the relation or is there a part that I missed in my Java code?
add the following in application.properties n check:---(there are multiple way to fix it one of I am writing down)
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
add dependencies:---
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.