Multiple Blobs in One Row using Spring Boot - spring

I want to save two images as byte array in single MySQL record using spring boot.
Here is my #Respository object. But that does not create the second blob.
Any ideas?
public class CustomerDetails {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "email")
private String emailAddress;
#Lob
#Column(name = "id_image")
private byte[] idImage;
#Lob
#Column(name = "signature_image")
private byte[] sugnatureImage;
}

Related

Posting Integer array in postgress through Postman on spring boot JPA service

I am trying to post the below request through postman in the item table to a spring boot service . I am using JPA , Postgress , Spring boot .
{
"allGroups" : [1,2,3],
"assetName" : "testname"
}
The Entity class is like below for the Asset table
public class Asset implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#Column(name = "id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String assetName;
#Type(type = "int-array")
#Column(name = "all_groups")
private Integer[] allGroups;
}
And the corresponding DTO is
#JsonInclude(JsonInclude.Include.NON_NULL)
public class AssetDto {
private long serialVersionUID;
#Max(Integer.MAX_VALUE)
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#Size(max = 255)
#NotBlank(message = "assetName may not be empty")
private String assetName;
private Integer[] allGroups;
}
When i am posting it is saving the "assetName" property but not the "allGroups" array . How to make it work submit array also .

Join Two Tables without foreign keys in Spring Boot with similar Ids

Here I have two tables; both have IDs as primary keys. I want to know how to join these tables without foreign keys, based on their IDs. What should be the service implementation and what should be in the repository? How to write #Query with JOINS?
#Entity
#Table(name = "procedures")
#JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Procedure implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ProcedureId")
private int id;
#Column(name = "ProcedureName")
private String name;
#Column(name = "ProcedureCode")
private String code;
#Column(name = "ProcedureDesc")
private String desc;
// getters and setters
}
#Entity
#Table(name = "cliniciandescriptor")
public class CPTClinicianDescriptor {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Id")
private int id;
#Column(name = "ConceptId")
private int conceptId;
#Column(name = "CPTCode")
private String cptCode;
#Column(name = "ClinicianDescriptorId")
private int clinicianDescriptorId;
#Column(name = "ClinicianDescriptor")
private String clinicianDescriptor;
// getters and setters
}
You can use the JOIN on syntax like in SQL
For example
select p from Procedure p join CPTClinicianDescriptor c on c.id = p.id;
Read more about that topic here:
https://72.services/how-to-join-two-entities-without-mapped-relationship/
Considering it as One-to-One relation, you can use something like this.
#Entity
#Table(name = "procedures")
#JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Procedure implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ProcedureId")
private int id;
#Column(name = "ProcedureName")
private String name;
#Column(name = "ProcedureCode")
private String code;
#OneToOne(optional = false)
#JoinColumn(name = "id", updatable = false, insertable = false)
private CPTClinicianDescriptor descriptor;
#Column(name = "ProcedureDesc")
private String desc;
// getters and setters
}

I am not getting unique column using hibernate , for postgresql db

#Entity
#Table(uniqueConstraints={#UniqueConstraint(columnNames={"catgoryId","applcationNo"})})
I tried this explicitly #table
and unique , //but not getting result.
public class DmsDocDetailPojo {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(columnDefinition = "serial")
private Long dmsDocId;
#Column
private String stateCode="AI";
#Column(name = "applicationNo", unique = true,nullable=false)
#NotNull
private String applcationNo;
#Column(name = "catgoryId", unique = true,nullable=false)
private String catgoryId;
#CreationTimestamp
#Column( nullable = false, updatable=false)
private Date doc_uploaded_dt;
#UpdateTimestamp
private Date doc_updated_dt;
#Column(name = "document_file", columnDefinition = "BYTEA")
private byte[] document_file;
#Column
private String fileName;
#Column
private Integer fileSize;
}
check your database primary key it should be an auto increment

How to add conditional based #NotNull constraint in spring boot?

We are using spring-boot.
I want to add conditional based constraint in java class.
For e.g.
#Entity
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name = "user_id")
private Integer userId;
#Column(name = "firstName")
private String firstName;
#Column(name = "lastName")
private String lastName;
}
Now in above code I want to put #NotNull constraint on lastName property if and only if the firstName property is not empty.

Hibernate, class property that is not binded to the table

I have such problem: i have a property at the class Test which should be not mapped to the column of the table (i use it after my Test object is loaded to determine should Test object be deleted or not). In my case this property is boolean delete:
#Entity
#Table(name = "[NewMVC].[dbo].[Tests]")
public class Test {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
#Column(name = "testQuestion")
private String testQuestion;
#Column(name = "optionOne")
private String optionOne;
#Column(name = "optionTwo")
private String optionTwo;
#Column(name = "optionThree")
private String optionThree;
#Column(name = "subjectType")
private int subjectType;
#Column(name = "correctOptionNumber")
private int correctOptionNumber;
private boolean delete = false;
....
How should i tell hibernate that my property delete is not mapped to the column?
You could annotate it with the #Transient annotation:
#Transient
private boolean delete = false;
This tells Hibernate to skip this attribute and not to generate a column.
Using the #Transient annotation:
#Transient
private boolean delete;

Resources