Spring boot JPA - primary key of entities in database - sql

I have #OneToMany and #ManyToOne relationship between parent and child entities.
#Entity
#Table(name = "parent")
public class Parent {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Long id;
#OneToMany(targetEntity=Measurement.class, mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
#PrimaryKeyJoinColumn
#JsonManagedReference
private List<Child> children = new ArrayList<>();
}
and I have child entity like this
#Entity
#Table(name = "child")
public class Child {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne(targetEntity=Parent.class, fetch = FetchType.LAZY)
#JoinColumn(name = "parent_id")
#JsonBackReference
private Parent parent;
After saving this data, the database looks like following,
Parent:
id
1
Child
id parent_id
2 1
my question is, why does the primary key of child is 2 and not 1? In the child table, it could have a primary key as 1 and foreign key reference to parent as 1. When I add one more parent, then the tables looks like this,
Parent:
id
1
3
Child
id parent_id
2 1
4 3
Am I doing something wrong or it is the expected behaviour?

The generation strategy #GeneratedValue(strategy = GenerationType.AUTO) will takes value from the default hibernate sequence, so the same sequence is used for all key generation.
When you are inserting the data, it inserts in the sequence like Parent then Child, as it is getting values from the same sequence generator, it will increase in sequence for all your table.
You need to use the GenerationType.IDENTITY strategy for your purpose.
In parent Parent
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
In child
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

Related

Foreign key constraint violation when deleting audited Hibernate envers Entity

I have a situation that looks like the following :
Two entities Employee and EmployeeHty :
#Entity
#Table(name = "employee")
#Audited
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Employee {
#Id
#GeneratedValue
#Column(name = "id")
private Long id;
#Column(name = "name")
private String name;
#ManyToOne
#JoinColumn(name = "employee_id")
private Employee employee;
}
#Entity
#Table(name = "employee_hty")
#Data
#AllArgsConstructor
#NoArgsConstructor
public class EmployeeHty {
#EmbeddedId
private HtyCompositePrimaryKey htyCompositePrimaryKey;
#Column(name = "revision_type")
private Integer revisionType;
#Column(name = "name")
private String name;
#ManyToOne
#JoinColumn(name = "employee_id")
private Employee employee;
}
#Embeddable
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#EqualsAndHashCode
public class HtyCompositePrimaryKey implements Serializable {
#Column(name = "id")
private Long id;
#ManyToOne
#JoinColumn(name = "revision_id")
private EmployeeAuditRevision auditRevision;
}
#Entity
#Table(name = "employee_audit_revision")
#Data
#AllArgsConstructor
#NoArgsConstructor
#RevisionEntity(EmployeeRevisionListener.class)
public class EmployeeAuditRevision {
#Id
#GeneratedValue
#RevisionNumber
#Column
private Long revisionId;
#RevisionTimestamp
#Column
private Date revisionDate;
#Column
private String userUid;
#ManyToOne
#JoinColumn(name = "user_id")
private User user;
}
An employee could have himself as boss. So i end up with this values in my database :
employee
id
name
employee_id
1
Jack
1
2
Melanie
null
employee_hty
id
revision_id
revision_type
name
employee_id
1
102
0
Jack
1
2
103
0
Melanie
null
revision types :
0 : created
1: updated
2: deleted
I want to delete manually Jack from the employee table and add a delete revision using SQL
INSERT INTO employee_hty VALUES (1, 104, 2, Jack, 1);
DELETE FROM employee WHERE id = 1;
Here is the situation i would like to be in :
employee
id
name
employee_id
2
Melanie
null
employee_hty
id
revision_id
revision_type
name
employee_id
1
104
2
Jack
1
1
102
0
Jack
1
2
103
0
Melanie
null
But since employee_hty has a foreign key constraint that link the column employee_id(table employee_hty) to the column id(table employee) it is not possible to do this. And i would like to have your thoughts on the matter.
Is deleting the foreign key constraint could be the solution ?
*NB: some details were omitted for the purpose of simplicity. *
Either remove the constraint or don’t populate the column for deletes

JPA Query mapping

I want to use orderNumber in ProductRepository but I keep getting SQL error. I mapped both sides.
this is product entity.
#Entity
public class Product extends BaseEntity {
#Id
#GeneratedValue
#Column(name = "product_id")
private Long id;
#ManyToOne
#JoinColumn(name = "orderItem_id")
private OrderItem orderItem;
this is orderItem entity.
#Entity
public class OrderItem extends BaseEntity {
#Id
#GeneratedValue
#Column(name = "orderItem_id")
private Long id;
private String orderNumber
#OneToMany(mappedBy = "orderItem", cascade = CascadeType.ALL, orphanRemoval = true)
#Builder.Default
private List<Product> products = new ArrayList<Product>();
this is query in ProductRepository.
#Query(value = "SELECT * FROM OrderItem a, Product b WHERE a.orderItem_id = b.orderItem_id", nativeQuery = true)
Product findByOrderNumber(String orderNumber);
Alternatively, you can try query by a method as shown below, you don't need to use the #Query in this case.
ProductRepository
Product findByOrderItemOrderNumber(String orderNumber);
you can find more details here

How to implement bidirectional hibernate relationship correctly?

#Entity
#Table(name = "project")
public class Project {
#Id
#Column(name = "project_id")
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#OneToOne
#JoinColumn(name = "idea_id", referencedColumnName = "idea_id")
#JsonIgnore
private Idea idea; .......}
And
#Entity
#Table(name = "idea")
public class Idea {
#Id
#Column(name = "idea_id")
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Size(max = 240)
#NotNull
private String description;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "project_id", referencedColumnName = "project_id")
private Project project; ..........}
What is wrong with my hibernate relationships in these two classes? This is example of relationship that I want to implement.
And this is actual tables that I get with these relationships:
Idea table:
And Project table:
How to get rid of null values in idea_id column in project table?

Making SQL/JPQL query to select all topics that matches both keywords

Challenge:
I want to create a query that selects Topics that match both the keyword ID´s "Java" and "sql", the Topic with ID = 1 matches both keywords "Java" and "Sql.
I have implemented two simple entities Topic and Keyword. They have a bidirectional ManyToMany relationship:
#Entity
#Table(name = "Topic")
public class Topic implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "topicID")
private Long id;
#Column(name = "topicTitle")
private String title = "";
#ManyToMany
#JoinTable(name = "Join_Topic_Keyword",
joinColumns = #JoinColumn(name = "Topic_ID"),
inverseJoinColumns = #JoinColumn(name = "Keyword_ID"))
private Set<Keyword> keywords;
}
#Entity
#Table(name = "Keyword")
public class Keyword implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "keywordID")
private String id;
#ManyToMany(mappedBy = "keywords")
private Set<Topic> topics;
}
Here is the structure table.
And This is the result from Join_Topic_Keyword

Multiple unidirectional oneToMany relationship columns from one table to another table in JPA

I'm trying to have a localization table that is linked to from multiple tables.
I'm realizing that the problem is that I'm using the ID of Localization (eg Localization_Id) so I can't link to different localizations without some other key. Should I use a join table or some other sequential id in the database somehow? Not sure what the best approach is using JPA.
Thanks in advance.
#Entity
public class MyEntityWithLocalization {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id;
#OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
#JoinColumn(name="LOCALIZATION_KEY")
List<Localization> field1;
#OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
#JoinColumn(name="LOCALIZATION_KEY")
List<Localization> field2; //can't be unique from field one as it links to the MyEntityWithLocalization id.
#OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL})
#JoinColumn(name="LOCALIZATION_KEY")
List<Localization> field3; //can't be unique from field one as it links to the MyEntityWithLocalization id.
}
#Entity
public class Localization {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id;
String language;
String string;
public Localization(String language, String string) {
this.language = language;
this.string = string;
}
public Localization(){
}
}
This creates a localization_key in the localization table but that is just keyed to the ID of the MyEntityWithLocalization - it needs to be another unique value which makes me believe a join table may make sense in this case.
create table localization (
id number(19,0) not null,
language varchar2(255),
string varchar2(255),
localization_key number(19,0),
primary key (id)
);
Hmm. Why not just split the entity into constant and localizable part? Something like this:
#Entity
class MyEntity{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id;
#OneToMany
#MapKeyColumn
Map<String, MyEntityLocalization> localizations;
}
#Embeddable
class MyEntityLocalization {
String field1;
String field2;
String field3;
}
Where the localizations field has the map from the language to the localization? The other way is using Hibernate-specific annotations:
#Entity
class MyEntityWithLocalization {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id;
#OneToMany
#MapKeyColumn(name="language")
#WhereJoinTable(clause = "key=1")
Map<String, String> field1;
#OneToMany
#MapKeyColumn(name="language")
#WhereJoinTable(clause = "key=2")
Map<String, String> field2;
}
#Entity
public class Localization {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
long id;
long key;
String language;
String string;
}