How to set entity relation mapping for two entites of same type inside another - spring

Example: I have a Spring Boot, HSQLB, JPARepository Project with
#Entity
public class LetterEntity{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private AddressEntity fromAddress;
private AddressEntity toAddress;
}
#Entity
public class AddressEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private int id;
private LetterEntity letter;
}
So, each Letter have two fields of type AddressEntity. Each AddressEntity has a field of its LetterEntity.
How I need to set the relations mappings and cascading?

Related

How to create entity with self-referencing (with many To many mapping)

#Entity
public class Department {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#ManyToMany(mappedBy = "departements")
private List<Department> departements;
I don't know how to implement this association .In my situation I have the same entity mapped by itself by many to many

Error creating bean with name 'jpaMappingContext': Invocation of init method failed;

Click Link for pic
Please help I am stuck in error after error
Need to create a spring data model and pass the test case
Spring data models are
#Entity
public class Cart {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer cartId;
private Double totalAmount;
#OneToOne
#JoinColumn(name = "userId")
private User user;
#OneToMany(mappedBy = "cart")
private List<CartProduct> cartProducts;
//getter and setter and constructor
}
#Entity
public class CartProduct {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer cpId;
#ManyToOne
#JoinColumn(name = "cartId")
private Cart cart;
#ManyToOne
#JoinColumn(name = "productId")
private Product product;
private Integer quantity = 1;
//getter and setter and constructor
}
#Entity
public class Category {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer categoryId;
private String categoryName;
//getter and setter and constructor
}
#Entity
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer productId;
private String productName;
private Double price;
#ManyToOne
#JoinColumn(name ="userId")
private User seller;
#ManyToOne
#JoinColumn(name ="userId")
private Category category;
//getter and setter and constructor
}
The main issue is role and user can't able to create
many-to-many relations
#Entity
public class Role {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
Integer roleId;
String role;
#ManyToMany(mappedBy = "roles")
Set<User> users = new HashSet<>();
//getter setter and constructor
}
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer userId;
private String username;
private String password;
// Implement Roles
#ManyToMany
#JoinTable(name="user_Role",joinColumns = #JoinColumn(name="userId")
,inverseJoinColumns = #JoinColumn(name="roleId"))
private Set<Role> roles = new HashSet<Role>();
}
https://pastebin.com/T34SbwMy
the test case is in the link and can't able to pass the test case.
Thanks in advance.

#OnDelete not working correctly with #Inheritance and #ManyToOne combination

When I delete the D object I would like to delete associated data in C(including parent data) as well. In the below code when I deleted the D, C part deleted successfully but the parent(A) related part still exists in the database.
I have seen the https://hibernate.atlassian.net/browse/HHH-13299 issue, but I don't know it is the same issue or not, if yes then what could I do as a workaround?
#Entity
class D {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
}
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
public abstract class A {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
private String common;
}
#Entity
public class B extends A {
}
#Entity
public class C extends A {
#ManyToOne(optional = false, cascade = CascadeType.ALL)
#JoinColumn(name = "d_id")
#OnDelete(action = OnDeleteAction.CASCADE)
private D d;
}
Assume that getter and setters are implemented.

Get data from multiple tables using one Mapping Table in Spring Boot

Procedure.java
#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
}
CPTClinicianDescriptor
#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
}
CPTProcedures
#Entity
#Table(name = "cptprocedures")
public class CPTProcedures {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Id")
private int Id;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "ProcedureId")
private Procedure procedure;
#Transient
private int procedureId;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "CPTCodeSet")
private CPTClinicianDescriptor cptClinicianDescriptor;
#Transient
private int cptCodeSet;
// getters and setters
}
Here I have 3 table in mySql in those 2 are different separate tables, I created one table for Mapping that has foreign keys of Procedures and CPT when i get a list using Procedure It should get corresponding cpt data also.
ServiceImpl Like:
public List<Procedure> getCombinedProcedureList(int id) {
// TODO Auto-generated method stub
List<Procedure> pList = new ArrayList<Procedure>();
Procedure procedureList = procedureRepository.findCombById(id);
pList.add(procedureList);
return pList;
}
When i use
Repository like
#Query(value = "SELECT * FROM procedures JOIN cliniciandescriptor ON cliniciandescriptor.Id = ?1",nativeQuery = true)
Procedure findCombById(int id);
this query in Procedures Repository it is getting only procedures. So, what can i do to get the combined(joined) list of 2(procedures & CPT) tables using 3rd mapping table.

Building several one-to-many relationships

It implements the model as seen below:
I implement three classes of models:
#Entity
public class Home implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(mappedBy = "home")
private Set<UserHome> userHomes;
}
#Entity
public class UserHome implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne
#JoinColumn(name = "home_id")
private Home home;
#OneToMany(mappedBy = "userhome")
private Set<Key> keys;
}
#Entity
public class Key implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne
#JoinColumn(name = "userhome_id")
private UserHome userHome;
}
When you try to compile it gets an error:
Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.homeUser.Key.userhome in com.example.homeUser.UserHome.keys
I do not know what's wrong with my code?
There is a typo in your code, the lowercase h in userhome:
#OneToMany(mappedBy = "userhome")
private Set<Key> keys;
Should be (uppercase H):
#OneToMany(mappedBy = "userHome")
private Set<Key> keys;
The fields/properties you reference in a field like mappedBy should have the exact name and case of the field in the JavaBean.

Resources