I have following entity-
package com.insurance.insurancebackend.entity;
import jakarta.persistence.*;
import lombok.Data;
import java.util.HashSet;
import java.util.Set;
@Data
@Entity
@Table(name="user")
public class User {
@Id
@Column (name ="userid" )
@GeneratedValue(strategy = GenerationType.AUTO)
private long userId;
@Column(name="firstname",nullable = false)
private String firstName;
@Column(name="lastname",nullable = false)
private String lastName;
@Column(name = "useremail",nullable = false,unique = true)
private String email;
@Column(name = "phonenumber",nullable = false,unique = true)
private long phoneNumber;
@OneToMany(mappedBy = "user",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private Set<UserPolicyQuery> userPolicyQueries = new HashSet<>();
}
and
@Data
@Entity
@Table(name="user_policy_query")
public class UserPolicyQuery {
@Id
@Column(name="query_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long queryId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="user_id",nullable=false)
private User user;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name="query_policy_county",
joinColumns = @JoinColumn(name="query", referencedColumnName = "query_id"),
inverseJoinColumns = @JoinColumn(name="policy_county", referencedColumnName = "policy_county_id"))
private Set<PolicyCounty> policyCounties=new HashSet<>();
@Column(name="comment",nullable = true)
private String comment;
}
When I am calling the method- (annotated with @Transactional)
@Transactional
@Override
public long adNewUserQueries(UserDto userDto) {
User user=this.userRepository.findByEmail(userDto.getEmail());
if(user==null){
user=new User();
}
user.setEmail(userDto.getEmail());
user.setFirstName(userDto.getFirstName());
user.setLastName(userDto.getLastName());
user.setPhoneNumber(userDto.getPhoneNumber());
UserPolicyQuery userPolicyQuery=new UserPolicyQuery();
userPolicyQuery.setComment(userDto.getComment());
Set<Long> policyCountyIds=userDto.getPolicyCountyIds();
for(Long policyCountyId: policyCountyIds){
PolicyCounty policyCounty=new PolicyCounty();
policyCounty.setPolicyCountyId(policyCountyId);
userPolicyQuery.getPolicyCounties().add(policyCounty);
}
user.getUserPolicyQueries().add(userPolicyQuery);
User savedUser=this.userRepository.save(user);
System.out.println(savedUser.getUserPolicyQueries());
return savedUser.getUserPolicyQueries().size();
}
I get the following error-
org.hibernate.PropertyValueException: not-null property references a null or transient value : com.insurance.insurancebackend.entity.UserPolicyQuery.user
If I remove (nullable=false) in
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="user_id",nullable=false)
private User user;
The foreign key column (user_id) gets set to null in the table. I am not able to understand why this is happening. When trying to save the user, the user details are saved, comment in the UserPolicyQuery table is saved, but the foreign key column is left null. Please help!
Not sure if you want to add a reference in "UserPolicyQuery" for User as there can be many Users having this policy.
Better if we start from db level, So joining table should be look like:
Then for the classes: User:
And on Role class there is no connection to the User. Think of Role class as UserPolicyQuery