Spring JPA saving distinct entities with composite primary key not working as expected, updates same entity - spring

I have a logic that saves some data and I use spring boot + spring data jpa.
Now, I have to save one object, and after moment, I have to save another objeect.
those of object consists of three primary key properties.
- partCode, setCode, itemCode.
let's say first object has a toString() returning below:
SetItem(partCode=10-001, setCode=04, itemCode=01-0021, qty=1.0, sortNo=2, item=null)
and the second object has a toString returning below:
SetItem(partCode=10-001, setCode=04, itemCode=01-0031, qty=1.0, sortNo=2, item=null)
there is a difference on itemCode value, and itemCode property is belonged to primary key, so the two objects are different each other.
but in my case, when I run the program, the webapp saves first object, and updates first object with second object value, not saving objects seperately.
(above image contains different values from this post question)
Here is my entity information:
/**
* The persistent class for the set_item database table.
*
*/
#Data
#DynamicInsert
#DynamicUpdate
#Entity
#ToString(includeFieldNames=true)
#Table(name="set_item")
#IdClass(SetGroupId.class)
public class SetItem extends BasicJpaModel<SetItemId> {
private static final long serialVersionUID = 1L;
#Id
#Column(name="PART_CODE")
private String partCode;
#Id
#Column(name="SET_CODE")
private String setCode;
#Id
#Column(name="ITEM_CODE")
private String itemCode;
private Double qty;
#Column(name="SORT_NO")
private int sortNo;
#Override
public SetItemId getId() {
if(BooleanUtils.ifNull(partCode, setCode, itemCode)){
return null;
}
return SetItemId.of(partCode, setCode, itemCode);
}
#ManyToMany(fetch=FetchType.LAZY)
#JoinColumns(value = {
#JoinColumn(name="PART_CODE", referencedColumnName="PART_CODE", insertable=false, updatable=false)
, #JoinColumn(name="ITEM_CODE", referencedColumnName="ITEM_CODE", insertable=false, updatable=false)
})
private List<Item> item;
}
So the question is,
how do I save objects separately which the objects' composite primary keys are partially same amongst them.
EDIT:
The entity extends below class:
#Setter
#Getter
#MappedSuperclass
#DynamicInsert
#DynamicUpdate
public abstract class BasicJpaModel<PK extends Serializable> implements Persistable<PK>, Serializable {
#Override
#JsonIgnore
public boolean isNew() {
return null == getId();
}
}
EDIT again: embeddable class.
after soneone points out embeddable class, I noticed there are only just two properties, it should be three of it. thank you.
#Data
#NoArgsConstructor
#RequiredArgsConstructor(staticName="of")
#Embeddable
public class SetGroupId implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
#NonNull
private String partCode;
#NonNull
private String setCode;
}

Check howto use #EmbeddedId & #Embeddable (update you might need to use AttributeOverrides in id field, not sure if Columns in #Embeddable works).
You could create class annotated #Embeddable and add all those three ID fields there.
#Embeddable
public class MyId {
private String partCode;
private String setCode;
private String itemCode;
}
Add needed getters & setters.
Then set in class SetItem this class to be the id like `#EmbeddedId´.
public class SetItem {
#EmbeddedId
#AttributeOverrides({
#AttributeOverride(name="partCode",
column=#Column(name="PART_CODE")),
#AttributeOverride(name="setCode",
column=#Column(name="SET_CODE"))
#AttributeOverride(name="itemCode",
column=#Column(name="ITEM_CODE"))
})
MyId id;
Check also Which annotation should I use: #IdClass or #EmbeddedId

Be sure to implement equals and hashCode in SetGroupId.
Can you provide that class?

Related

JPARepository CPRQ modified does not save full object

I have modified the design of CPRQ a bit to help my database pattern
I have an Employee table and a Department table. Both have common properties
#Column(name="tenantIDPKFK")
private Integer tenantIdpkfk;
#Column(name="status")
private Integer status;
So I created a base class ABaseEntity like below
public class ABaseEntity {
public ABaseEntity() {
}
public ABaseEntity(int tenantIdpkfk, int status) {
this.tenantIdpkfk = tenantIdpkfk ;
this.status = status ;
}
#Column(name="tenantIDPKFK")
private Integer tenantIdpkfk;
#Column(name="status")
private Integer status;
I have extended EmployeeEntity with ABaseEntity
#Entity
#Table(name = "employee")
public class EmployeeEntity extends ABaseEntity{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "first_name")
#NotEmpty(message = "Please provide a name")
#NotBlank
private String firstName;
My CommandHandler runs the following code
EmployeeEntity savedEmployeeEntity = this.employeeRepository.saveAndFlush(employee);
this.mediator.emit(new EmployeeCreatedEvent(savedEmployeeEntity.getId()));
Database saved the object, but only id, firstname. Does not save tenant and status columns.
I know I am missing something silly. Please help.
EDIT
Adding #MappedSuperclass to the ABaseEntity class fixed the issue.
#MappedSuperclass
public class ABaseEntity {...}
Database saved the object, but only id, firstname. Does not save
tenant and status columns.
By default JPA doesn't consider the parent class in the orm (object-relational mapping) of the current class.
You have to specify on the parent class #Inheritance with the strategy to use or use the default one.
For example :
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class ABaseEntity {...}
More info here.

How can I add a tenant condition to Spring Data JPA Default and Dervied Queries

I have a Springboot Application with Repositories having Spring Data JPA Queries like findOne, findAll and also derived ones like findByID or findByName etc.
What I want to achieve is multitenancy. All entities have an "account_id" column which holds the tenant.
How do I add a filter like "account_id" to all the queries metioned above without using derived queries that contains those name slike findIdAndAccountid (which would be findone)
#Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {
Category findByName(String name);
}
Here's the corresponding entity
#Entity
#Table(name = "unit")
#Data
public class Unit {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
#Column(name = "account_id")
private Long account_id;
}
I know most people use schemas as tenant separation but that's impossible for me. Is there a way (I didn't find one) to add such a tenant filter condition on those queries without writing NamedQueries or using DerivedQueries. An elegeant solution like annotate the repository or entity or maybe the queries that all queries should add the additional filter "account_id"?
You can add Where clause on your Entity classes (Didnt had time to test )
#Entity
#Table(name = "unit")
#Data
#Where(clause = "account_id= :account_id")
public class Unit {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
#Column(name = "account_id")
private Long account_id;
}
Update and Solution
1. Create a Filter & FilterDef on the entity like so
#FilterDef(name="accountFilter", parameters=#ParamDef( name="accountId", type="long" ) )
#Filters( {
#Filter(name="accountFilter", condition=":accountId = account_id")
} )
public class Category {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
#Column(name = "account_id")
private Long account_id;
}
enable filtering in the controller by autowiring entitymanager, writing a method to enable the filter and activate the filter in #ModelAttribute for each request
#RestController
#RequestMapping(path = "/categories",produces = MediaType.APPLICATION_JSON_VALUE )
public class CategoryController {
private final CategoryRepository repository;
#Autowired
private EntityManager entityManager;
CategoryController(CategoryRepository repository) {
this.repository = repository;
}
private void activateFilter() {
Session session = entityManager.unwrap(Session.class);
Filter filter = session.enableFilter("accountFilter");
filter.setParameter("accountId", Long.valueOf(TenantContext.getCurrentTenant()));
}
#ModelAttribute
public void initFilter() {
activateFilter();
}
... your rest methods here
}

Spring/JPA: composite Key find returns empty elements [{}]

I have build my data model using JPA and am using Hibernate's EntityManager to access the data. I am using this configuration for other classes and have had no problems.
The issue is that I created an entity with a composite primary key (the two keys are foreign keys) , adding elements works perfectly I checked it in database but I am not able to retrieve the populated row from database.
For example if I query "FROM Referentiel" to return a list of all referentiels in the table, I get this [{},{}] my list.size() has the proper number of elements (2), but the elements are null.
The entity:
#Entity
#Table(name = "Et_referentiel")
public class Referentiel implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#ManyToOne
#JoinColumn(name = "id_projet")
private Projet projet;
#Id
#ManyToOne
#JoinColumn(name = "id_ressource")
private Ressource ressource;
#Column(name = "unite", nullable = false)
private String unite;
}
here is my controller getList method:
#PostMapping(value = "/list", consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public List<Referentiel> listReferentiel(#RequestBody Long idProjet) {
List<Referentiel> referentiel = referentielService.listReferentiel(idProjet);
return referentiel;
}
and here is my dao methods:
#Autowired
private EntityManager em;
#Override
public void ajouterReferentiel(Referentiel ref) {
em.persist(ref);
em.flush();
}
#SuppressWarnings("unchecked")
#Override
public List<Referentiel> listReferentiel(Long idProjet) {
Query query = em.createQuery("Select r from Referentiel r where r.projet.idProjet=:arg1");
query.setParameter("arg1", idProjet);
em.flush();
List<Referentiel> resultList = query.getResultList();
return resultList;
}
Any help is greatly appreciated.
Try creating a class representing your composite key:
public class ReferentielId implements Serializable {
private static final long serialVersionUID = 0L;
private Long projet; // Same type than idProjet, same name than inside Referentiel
private Long ressource; // Same type than idRessource (I guess), same name than inside Referentiel
// Constructors, getters, setters...
}
And assign it to your entity having that composite key.
#Entity
#IdClass(ReferentielId.class) // <- here
#Table(name = "Et_referentiel")
public class Referentiel implements Serializable {
// ...
}
Notice that it is required to have a class representing your composite keys, even if that does not help in your problem.

Spring data jpa persist nested changes

I have 3 classes:
Record/ Profile / Options
#Entity
#Table(name="Record")
public class Record implements Serializable {
#Id
#GeneratedValue
private int id;
#ManyToOne(cascade=CascadeType.MERGE)
#JoinColumn(name="ProfileId")
private Profile profile;
....
}
#Entity
#Table(name="Profile")
public class Profile implements Serializable {
#Id
#GeneratedValue
private int id;
#ManyToOne(cascade=CascadeType.MERGE)
#JoinColumn(name="OptionId")
private Option option;
....
}
#Entity
#Table(name="Option")
public class Option implements Serializable {
#Id
#GeneratedValue
private int id;
private String name;
....
}
let's say the original option is "50M" and then I change the record1.profile1.option to "10M"
Also when I do record1.setId(null);recordRepository.save(record1);
I want to create an new entry from record1(as a change history).
In this case because the option is nested, the cascade type of merge will not persist the changes happened in profile. Thus when I get the record back, it will still say that recordNew.profile1.option is 50M
But if I change the cascadeType to CascadeType.ALL or CascadeType.PERSISTin the Record class, when I try to save the new entry, it seems Spring Data JPA will complains about detached property as: org.hibernate.PersistentObjectException: detached entity passed to persist: com.test.lalala.profile.Profile
Is there a way that I could fix this?

hibernate Mapping One to many relation ship between primary key and composite key

I am struggling with a hibernate mapping problem of mapping One to many relation ship between Primary key of Order Table and composite key of Product Cart with some extra columns
public class OrderDetails implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#Column(name="ORDERID")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer orderId;
#Column(name="ORDER_DATE")
private Date orderDate= new Date();
//other fields and getter setter
.....
.....
Product Cart table has a composite key CART ID and PRODUCT ID
#Entity
#Table(name="PRODUCT_CART")
#AssociationOverrides({
#AssociationOverride(name="pk.shopCart", joinColumns=#JoinColumn(name="CARTID")),
#AssociationOverride(name="pk.product", joinColumns=#JoinColumn(name="PRODUCTID"))
})
public class ProductCart implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#EmbeddedId
private ProductCartId pk = new ProductCartId();
#Column(name="QUANTITY")
private Integer selectedQuantity=1;
#Column(name="TOTAL")
private double total=0.0;
//other fields and getter setter
.....
.....
I tried following but not working
#Entity
#Table(name="PRODUCTCART_ORDERDETAILS")
#AssociationOverrides({
#AssociationOverride(name="pcoPK.orderDetails",joinColumns=#JoinColumn(name="ORDERID")) ,
#AssociationOverride(name="pcoPK.pk", joinColumns=
{#JoinColumn(name="pk.shopCart",referencedColumnName="CARTID"),
#JoinColumn(name="pk.product",referencedColumnName="PRODUCTID") }) })
public class ProductCartOrder implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2348674131019001487L;
#EmbeddedId
private ProductCartOrderId pcoPK = new ProductCartOrderId();
#Column(name="QUANTITY")
private Integer quantity;
#Column(name="PRICE")
private double price;
#Transient
public OrderDetails getOrderDetails(){
return getPcoPK().getOrderDetails();
}
public void setOrderDetails(OrderDetails orderDetails){
getPcoPK().setOrderDetails(orderDetails);
}
#Transient
public ProductCartId getProductCartId(){
return getPcoPK().getPk();
}
public void setProductCartId(ProductCartId pk){
getPcoPK().setPk(pk);
}
Can someone please help me to implement this? Below is the error message
Caused by: org.hibernate.AnnotationException: Illegal attempt to define a #JoinColumn with a mappedBy association: pcoPK.pk
at org.hibernate.cfg.Ejb3JoinColumn.buildJoinColumn(Ejb3JoinColumn.java:152)
at org.hibernate.cfg.Ejb3JoinColumn.buildJoinColumns(Ejb3JoinColumn.java:127)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1212)
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:1841)
at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1878)
After lot of research I could not find the solution I done it in another way.
I created Many to many relationship between OrderDetails and Product with some extra columns ID, price, quantity and inserted value manually for each element in product cart thorugh a for loop.
public class Product implements Serializable {
#OneToMany(mappedBy="product")
private Set<ProductOrder> productOrder;
...//other fields and getter setter
}
public class OrderDetails implements Serializable {
#OneToMany(mappedBy="orderDetails")
private Set<ProductOrder> productOrder;
...//other fields and getter setter
}
public class ProductOrder {
#Id
#Column(name="PRODUCT_ORDER_ID")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int prductOrderId;
#ManyToOne
private OrderDetails orderDetails;
#ManyToOne
private Product product;
...//other fields and getter setter
}
In my controller class where I wanted to save the products of ProductCart I did following
List<ProductCart> productList = new ArrayList<ProductCart>();
productList=productCartService.getCartProducts(shopCart);
ProductOrder orderedProducts = new ProductOrder();
for (ProductCart productCarts : productList) {
orderedProducts.setOrderDetails(orderDetails);
orderedProducts.setProduct(productCarts.getPk().getProduct());
orderedProducts.setPrice(productCarts.getPk().getProduct().getPrice());
orderedProducts.setQuantity(productCarts.getSelectedQuantity());
productOrderService.addOrderProducts(orderedProducts);
}

Resources