org.postgresql.util.PSQLException: ERROR: insert or update on table violates foreign key constraint - postgresql

org.postgresql.util.PSQLException: ERROR: insert or update on table "party_custom_fields" violates foreign key constraint "fk21oqkpi7046skme7jce06fxdu"
Below error could help, what need to be done on the code, I have tried few reference, but not helpful.
Detail: Key (custom_field_value)=(11) is not present in table "custom_field_value"
Above is my error while saving.
Party is the class which will have custom fields and it's data
import lombok.Data;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
#Entity
#Data
#Table(name = "party")
public class Party {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#Column(name = "last_name")
private String lastName;
private String email;
private String gender;
#OneToMany(cascade = CascadeType.ALL)
#JoinTable(name = "party_custom_fields",
joinColumns = {#JoinColumn(name = "custom_field")},
inverseJoinColumns = {#JoinColumn(name = "custom_field_value")})
#MapKeyColumn(name = "custom_field_key")
private Map<Long, CustomFieldValue> customField = new HashMap<>();
public Party() {
}
public Party(String name) {
this.name = name;
}
}
Custom fields value model
package org.aeq.multitenant.model;
import lombok.Data;
import javax.persistence.*;
#Data
#Entity
#Table(name = "custom_field_value")
public class CustomFieldValue {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String value;
}
Custom fields model which hold what are the custom fields for the tables
package org.aeq.multitenant.model;
import lombok.Data;
import org.aeq.multitenant.enums.Tables;
import javax.persistence.*;
#Data
#Entity
#Table(name = "custom_field")
public class CustomField {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String type;
private boolean optional;
#Enumerated(EnumType.STRING)
#Column(name = "table_name")
private Tables tableName;
}
Controller function to save
Map<Long, CustomFieldValue> cfMap = new HashMap<>();
for (CustomField cf : customFields) {
if (!partyData.containsKey(cf.getName())) {
return new ApiResult<>(false, "Please provide " + cf.getName() + " custom field of party");
} else {
CustomFieldValue cfv = new CustomFieldValue();
cfv.setValue(partyData.get(cf.getName()).trim());
cfv = customFieldValueRepository.save(cfv);
cfMap.put(cf.getId(), cfv);
}
}
Party party = new Party();
party.setName(partyData.get("name"));
party.setEmail(partyData.get("email").trim());
party.setGender(partyData.get("gender").trim());
party.setLastName(partyData.get("last_name").trim());
party.setCustomField(cfMap);
party = partyRepository.save(party);
please review my code and let me where I am going wrong

If a column has a foreign key constraint, then any entry to that column should be present in the reference table given. If not, then this exception will be thrown.

Related

Joining three tables with #OneToMany association

I have three tables.
1.Street (street_id, settlement_entity_id, street_name),
2.Settlement_entity (settlement_entity_id, settlement_id, settlement_name), for example could be community, regional, municipality.. and
3.Settlement (settlement_id, settlement_name), for example could be city, town, township, village....
Relation - Street - Settlement_entity = > onetomany
Relation - Settlement_entity - Settlement = > onetomany
I want when I enter the name of the street, I get the settlement entity as above
as in parentheses and after that the settlement name with characteristics that are not to be the subject of this application, such as for example the house/apartment number, floor, longitude, latitude, altitude of the settlement.
entity
Street.java
package net.javaguides.springboot.entity;
#Entity
#Table(name = "street")
public class Street {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long street_id;
#Column(name = "street_name")
private String street_name;
public Street(String street_name) {
super();
this.street_name = street_name;
}
// getter and setters
entity
Settlement_entity.java
package net.javaguides.springboot.entity;
import javax.persistence.Column;
#Entity
#Table(name = "settlement_entity")
public class SettlementEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long settlement_entity_id;
#Column(name = "settlement_entity_name")
private String settlement_entity_name;
public SettlementEntity(String settlement_entity_name) {
super();
this.settlement_entity_name = settlement_entity_name;
}
// getters and setters
public void setSettlement_entity_name(String settlement_entity_name) {
this.settlement_entity_name = settlement_entity_name;
}
}
entity
Settlement.java
package net.javaguides.springboot.entity;
import javax.persistence.Column;
#Entity
#Table(name = "settlement")
public class Settlement {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long settlement_id;
#Column(name = "settlement_name")
private String settlement_name;
public Settlement(String settlement_name) {
super();
this.settlement_name = settlement_name;
}
// getters and setters
}
I creating according suggestion from -thelearner new linking entity which would be used for mapping that particular table.
entity linking table
StreetSettlementEntitySettlement.java
package net.javaguides.springboot.entity;
import javax.persistence.CascadeType;
public class StreetSettlementEntitySettlement {
#Id
#GeneratedValue
private long id;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinColumn(name = "street_id")
private Street street;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinColumn(name = "settlement_entity_id")
private SettlementEntity settlement_entity;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinColumn(name = "settlement")
private Settlement settlement;
public StreetSettlementEntitySettlement(long id, Street street, SettlementEntity
settlement_entity, Settlement settlement) {
super();
this.id = id;
this.street = street;
this.settlement_entity = settlement_entity;
this.settlement = settlement;
}
}
Directory structure
I saw some proposal from thelearner like this:
It's not clear for me and don't understanding. Where should I put those three classes or should do something else. Repo's is clear.
Any help and explanation would be appreciated. Thank you

Avoid recursion in one to many relation in jpa entities

I have two entities paymentTransaction and paymentCard as follows. When I fetch paymentTransactions It stuck in infinite call from one to another. I have tried #JsonIgnore, #JsonIdentityInfo and etc to sortout this issue but non of them works. I'm running select query as HQL query like this
FROM PassengersLogArchive as pass_log INNER JOIN " +
"PaymentTransactions as pmt_txn ON pmt_txn.tripId=pass_log.passengersLogId
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
#Data
#Table(name = "payment_transactions")
#NoArgsConstructor
#Entity
public class PaymentTransactions {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "trip_id")
private Long tripId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "card_id")
private PaymentCards paymentCard;
}
#Data
#Table(name = "payment_cards")
#NoArgsConstructor
#ToString
#Entity
public class PaymentCards {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "passenger_id")
private Long passengerId;
#Column(name = "masked_number")
private String maskedNumber;
#OneToMany(mappedBy = "paymentCard", fetch = FetchType.LAZY)
private List<PaymentTransactions> paymentTransactions;
}

Hibernate - Spring Data JPA One To Many unique constraint across table

I have a department and employee table. I need the dept name and employees in the department to be unique such that a new entry for the dept with the same set of employees is given it should throw a unique constraint violation error.
I am not sure how to work this out in hibernate.
I need to have unique constraints across multiple tables.
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
#Entity
#Table(
name="dept"
)
public class DeptEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(nullable = false)
private String name;
#OneToMany(mappedBy = "dep", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Employee> attributes = new ArrayList<>();
}
import javax.persistence.*;
#Entity
#Table(name="emp"
)
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
#ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
#JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
private DeptEntity dep;
}
You can use like :
#Column(unique= true)
private String name;

JPA Persist parent and child with one to many relationship

I want to persist parent entity with 20 child entities,
my code is below
Parent Class
#OneToMany(mappedBy = "parentId")
private Collection<Child> childCollection;
Child Class
#JoinColumn(name = "parent_id", referencedColumnName = "parent_id")
#ManyToOne(optional=false)
private Parent parent;
String jsonString = "json string containing parent properties and child collection"
ObjectMapper mapper = new ObjectMapper();
Parent parent = mapper.readValue(jsonString, Parent.class);
public void save(Parent parent) {
Collection<Child> childCollection = new ArrayList<>() ;
for(Child tha : parent.getChildCollection()) {
tha.setParent(parent);
childCollection.add(tha);
}
parent.setChildCollection(childCollection);
getEntityManager().persist(parent);
}
So if there are 20 child tables then I have to set parent reference in each of them for that I have to write 20 for loops?
Is it feasible? is there any other way or configuration where I can automatically persist parent and child?
Fix your Parent class:
#OneToMany(mappedBy = "parent")
mappedBy property should point to field on other side of relationship. As JavaDoc says:
The field that owns the relationship. Required unless the relationship is unidirectional.
Also you should explicitely persist Child entity in cycle:
for(Child tha : parent.getChildCollection()) {
...
getEntityManager().persist(tha);
...
}
As Alan Hay noticed in comment, you can use cascade facilities and let EntityManager automatically persist all your Child entities:
#OneToMany(mappedBy = "parent", cascade = CascadeType.PERSIST)
More details about cascades (and JPA itself) you can find in Vlad Mihalcea's blog.
Generally, #JoinColumn indicates that the entity is the owner of the relationship & mappedBy indicates that the entity is the inverse of the relationship.
So, if you are trying like following
#OneToMany(mappedBy = "parent")
private Collection<Child> childCollection;
That means it is inverse of the relationship and it will not set parent reference to its child.
To set parent reference to its child, you have to make the above entity owner of the relationship in the following way.
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn
private Collection<Child> childCollection;
You need not set any child reference because above code will create a column in the child table.
As pointed out in the comments you must take care of the object graph consistency with child/parent relationship. This consistency won't come free when JSON is coming directly from i.e. a POST request.
You have to annotate the parent and child field with #JsonBackReference and #JsonManagedReference.
Parent class:
#OneToMany(mappedBy = "parentId")
#JsonBackReference
private Collection<Child> childCollection;
Child class:
#JoinColumn(name = "parent_id", referencedColumnName = "parent_id")
#ManyToOne(optional=false)
#JsonManagedReference
private Parent parent;
Similar question with answer is here
Furthermore, if you use #JsonBackReference/#JsonManagedReference on javax.persistence annotated classes in combination with Lombok's #ToString annotation you will incur in stackoverflow error.
Just exclude childCollection and parent field from the #ToString annotation with #ToString( exclude = ...)
The same will happen with Lombok's generated equals() method (#Data, #EqualsAndHashCode). Just implements those methods by hand or to use #Getter and #Setter annotations only.
I would let the parent persist it's own children
package com.greg;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
#Entity(name = "PARENT")
public class Parent {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "NAME")
private String name;
#Column(name = "DESCRIPTION")
private String description;
#OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
#JoinColumn(name = "parent", referencedColumnName = "id", nullable = false)
private List<Child> children = new ArrayList<Child>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
}
I am using lombok to generate getter and setter properties on my entity classes.
I was also facing issue of NULL referenceID on child entity when I was trying to save parent Entity having child.
On my parent entity when I add children then I set "this" reference of parent on child.
In my example, I have User table and Address table where a User can have many addresses.
I have created domain classes as below.
e.g address.setUser(this);
package com.payment.dfr.entities;
import lombok.Data;
import javax.persistence.*;
import java.math.BigInteger;
#Entity
#Data
#Table(name="User")
public class User {
#Id
#GeneratedValue
private BigInteger RecordId;
private String Name;
private String Email;
#Getter(AccessLevel.NONE)
#Setter(AccessLevel.NONE)
#OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<Address> addresses = new ArrayList<>();
public void addAddress(Address address){
address.setUser(this);
addresses.add(address);
}
}
#Entity
#Data
#Table(name="UserAddress")
public class Address {
#Id
#GeneratedValue
private BigInteger RecordId;
private String AddressLine;
private String City;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name="UserId")
private User user;
}
This is how I save user with address
User newUser = new User();
newUser.setName("Papa");
newUser.setEmail("manish#gmail.com");
Address address1 = new Address();
address1.setAddressLine("4401 Central Ave");
address1.setCity("Fremont");
newUser.addAddress(address1);
Address address2 = new Address();
address2.setAddressLine("4402 Central Ave");
address2.setCity("Fremont");
newUser.addAddress(address2);
User user1 = userRepository.save(newUser);
log.info(user1.getRecordId().toString());

JPA OneToOne not working

I followed by tutorial : http://www.codejava.net/frameworks/hibernate/hibernate-one-to-one-mapping-with-foreign-key-annotations-example
I have following code:
#Entity
#Table(name = DomainConstant.TABLE_USER)
public class User{
#Id
#Column(name = DomainConstant.DOMAIN_USER_ID)
#GeneratedValue
private Long userId;
private UserActivationCode userActivationCode;
///////////////////// CONSTRUCTOR....
/// STANDARD GET AND SET....
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = DomainConstant.DOMAIN_ACTIVATION_LINK_ID)
public UserActivationCode getUserActivationCode() {
return userActivationCode;
}
}
#Entity
#Table(name = DomainConstant.TABLE_USER_ACTIVATON_LINK)
public class UserActivationCode {
#Id
#Column(name = DomainConstant.DOMAIN_ACTIVATION_LINK_ID)
#GeneratedValue
private Long userActivationCodeId;
#Column(name = DomainConstant.DOMAIN_ACTIVATION_DATE)
#Temporal(javax.persistence.TemporalType.DATE)
private Date date;
#Column(name = DomainConstant.DOMAIN_ACTIVATION_CODE)
private String code;
///////////////////// CONSTRUCTOR....
/// STANDARD GET AND SET....
}
When I save the User object it does not make record in UserActivationCode, why?
Like this:
User newUser = new User();
newUser.setUserActivationCode(new UserActivationCode("this is example"));
userDao.save(newUser);
I have record only in user table.
Can you tell me why?
Your problem is that you are mixing access types. In the User entity you have specified #Id on a field (private Long userId) whereas you have defined the join mapping on a property (the getter to UserActivationCode). If you specify the join mapping on the field, it should work as is.
#Entity
#Table(name = DomainConstant.TABLE_USER)
public class User{
#Id
#Column(name = DomainConstant.DOMAIN_USER_ID)
#GeneratedValue
private Long userId;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = DomainConstant.DOMAIN_ACTIVATION_LINK_ID)
private UserActivationCode userActivationCode;
///////////////////// CONSTRUCTOR....
/// STANDARD GET AND SET....
public UserActivationCode getUserActivationCode() {
return userActivationCode;
}
}
For more information on access and access types, see Access, Java EE 7