1

I have a model which i want to save to database.

@Data
public class Model {
 @Id
 private UUID id;
 private String name;
 private ModelSettings settings;
 @Data
 static class ModelSettings {
 boolean fuelEngine;
 }
}
create table model
(
 id uuid not null,
 name varchar(25) not null,
 settings jsonb
)

i try to save modelSettings as jsonb object using simple repository method save(), but i got error

ERROR: relation "settings" does not exist

i wrote custom Converter and i see when modelSettings is converted to json, but after prepare statement Spring Data try to save settings field to related table. How to tell Spring Data save field as json only, not row in related table?


Sorry, i forgot @WritingConverter with JdbcValue.

Machavity
31.8k27 gold badges97 silver badges108 bronze badges
asked Feb 21, 2022 at 8:21

2 Answers 2

1

Hi Please use Embedded Annotation: Embedded entities are used to have value objects in your java data model, even if there is only one table in your database.

@Data
public class Model {
 @Id
 private UUID id;
 private String name;
 @Embedded(onEmpty = USE_NULL)
 private ModelSettings settings;
 @Data
 static class ModelSettings {
 boolean fuelEngine;
 }
}
answered Feb 21, 2022 at 8:46
Sign up to request clarification or add additional context in comments.

3 Comments

@Embedded save settings fields as part of model entity. But i need whole settings object as json.
ERROR: column "fuel_engine" of relation "model" does not exist
could you please check this link: stackoverflow.com/questions/51276703/…
0

You can not have an object in your entity and expect JDBC to save it as json for you.

you need to define a String column and write a converter for it for saving in and reading from database.

also some databases like Oracle supports json values but you have not mentioned which database you are using.

in Oracle database you can define a table including a json column as below:

CREATE TABLE "USERS"
(
 "ID" NUMBER(16,0) PRIMARY KEY,
 "USER_NAME" VARCHAR2(85) UNIQUE NOT NULL,
 "PASSWORD" VARCHAR2(48) NOT NULL,
 "PROFILE" NCLOB NOT NULL CONSTRAINT profile_json CHECK ("PROFILE" IS JSON),
 "SETTINGS" NCLOB NOT NULL CONSTRAINT settings_json CHECK ("SETTINGS" IS JSON), 
 
);

And you need to create your entity class as below:

@Entity
@Table(name = "Users")
public class User {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_GENERATOR")
@SequenceGenerator(name = "USER_GENERATOR", sequenceName = "USERS_SEQ", allocationSize = 1)
private Long id;
@Column(name = "USER_NAME")
private String userName;
 
@Column(name = "PASSWORD")
private String password;
@Lob
@Nationalized
@Column(name = "PROFILE",columnDefinition="NCLOB NOT NULL")
private String profile;
@Lob
@Nationalized
@Column(name = "SETTINGS",columnDefinition="NCLOB NOT NULL")
private String settings;
}

as you can see here profile and setting are my json columns.

answered Feb 21, 2022 at 11:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.