'Manage unique fields insertion with hibernate
I'm working on a JAVA project and need to insert an entity in my DB using Hibernate but the fields need to be inserted only once.
I have this ProjectEntity class that I need to insert to my database :
@Entity
@Setter
@Getter
public class ProjectEntity extends PanacheEntity {
private String projectname;
private String projectkey;
}
My projectentity table looks like this :
CREATE TABLE projectentity
(
id BIGINT NOT NULL
CONSTRAINT project_pkey
PRIMARY KEY,
projectname TEXT,
projectkey TEXT,
);
I tried to put the fields in the table as unique but obviously it is not enought as I get an error when I'm trying to insert the same fields and it blocks the execution of my program.
I want to know how can I manage the insertion of the project entity once when a new project is specified and then not do it again if the project already exists in my database.
Solution 1:[1]
You can use @Column for every fields and set unique=true .
example :
@Column (name "projectname",nullable = false, unique = true )
private String projectname;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pirooz Azarabad |