Spring Boot JPA join columns with a partial composite key - java

I have entity classes like this structure:
Class Parent {
#EmbededId
private ParentId id;
#OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="parentDetails")
private List<Child> childDetails;
...
}
#Embeddable
Class ParentId {
private Integer pid1;
private Integer pid2;
private Integer pid3;
...
}
Class Child {
#EmbededId
private ChildId id;
#ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
#JoinColumns({
#JoinColumn(name="C_ID1" referencedColumnName="P_ID1")
#JoinColumn(name="C_ID2" referencedColumnName="P_ID2")
)}
private Parent parentDetails;
...
}
#Embeddable
Class ChildId {
private Integer cid1;
private Integer cid2;
private date cid3; // its a totally different field
...
}
I don't have any relationship with pid3 and cid3 as they are different. If I go with above design I am getting below error:
org.hibernate.AnnotationException: referencedColumnNames(P_ID1, P_ID2) of Child.parentDetails referencing Parent not mapped to a single property
If I comment pid3 then it works. So does that mean that I can't refer part of composite key as join columns? Is there any solution for it? I can't make changes to tables as they are legacy.

Related

How to use Optimistic Locking between 3 entities in JAVA

I have the following entities:
#Entity
public class TableA {
#Id
#GeneratedValue(...)
private Long id;
private Timestamp updatedDateTime;
#Version
private int version;
}
#Entity
public class TableB{
#Id
#GeneratedValue(...)
private Long id;
#ManyToOne
#JoinColumn(name="fieldTableC")
private TableC paymentsData;
}
#Entity
public class TableC{
#Id
#Column(name = "fieldTableC")
private String fieldTableC;
private String someOtherField;
}
The problem I am facing, is that I cannot find any information on how to do the following scenario:
If either TableB or TableC gets updated, then I need hibernate to automatically increment the version in TableA. Should I add a FK of TableA to TableB & TableC ?
As you can see, I already have a #ManyToOne from TableB to TableC, if that helps in any way.
PS: I am using JpaRepository
I ended up by adding the child reference in parent, and parent reference into child. Whenever I want to save a child, I also update the parent updatedDateTime value. My method is #Transient, so everything will be saved in 1 transaction.
Also added CascadeType.SAVE_UPDATE, so that when saving parent, the child will also be saved
Eg:
Added to TableA:
#OneToMany(mappedBy = "tableA")
#Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
private List<TableB> list= new ArrayList<>();
Added to TableB:
#ManyToOne
#JoinColumn(name = "tableA_root_id")
private TableA tableA;

Problems with Relationship Composite Primary Key on Hibernate

I have this diagram
How can I create the relationship on Hibernate of the entities
EscalaDetalhes and DefinicaoEscala, because on DefiniciaoEscala entity there is a composite primary key formed by idDetalhe (foreign with (EscalaDetalhes)) and idTurma(foreign with (Turma)).
I've already done the class like below :
#Embeddable
public class DefinicaoEscalaPK implements Serializable {
#Column(name="myColumn")
private Integer idTurma;
#Column(name="myColumn2")
private Integer idEscalaDia;
//GETTERS , SETTERS , HASH AND EQUALS
}
#Entity
public class DefinicaoEscala implements Serializable {
#EmbeddedId
private DefinicaoEscalaPK id;
#ManyToOne()
#JoinColumn(name = "idTurno")
private TurnoBean turno;
//GETTERS , SETTERS , HASH AND EQUALS
}
#Entity
#Table(name ="table")
public class EscalaDetalhes{
#id
private Integer idDetalhe;
#ManyToOne()
#JoinColumn(name="mybdColumn")
private EscalaBean escala;
#Column(name="myColumn")
private Calendar dia;
//MY QUESTION IS HERE , HOW WOULD I DESCRIBE THE RELATION HERE?
private List<DefinicaoEscala> escalaDiaDetalhes;
//GETTERS , SETTERS , HASH AND EQUALS
}
My question is here, How can I describe the relation?
private List<DefinicaoEscala> escalaDiaDetalhes;
referencedColumnName is basically used to convey that which column of the other entity will be mapped could be used for mapping in the current entity, thus, you can use the property referencedColumnName while mentioning the relationship:
#Entity
#Table(name ="table")
public class EscalaDetalhes{
#id
private Integer idDetalhe;
#ManyToOne()
#JoinColumn(name="mybdColumn")
private EscalaBean escala;
#Column(name="myColumn")
private Calendar dia;
//MY QUESTION IS HERE , HOW WOULD I DESCRIBE THE RELATION HERE?
#OneToMany
#JoinColumn(name="idDetalhe",referencedColumnName="idEscalaDia")
private List<DefinicaoEscala> escalaDiaDetalhes;
//GETTERS , SETTERS , HASH AND EQUALS
}
P.S: It is entirely on the basis of code you have shared.

how to select child in one to many relation in jpa

I want to select parent with child that i want.
But when I select my parent I have to show all the childs
How can i do that?
Example:
public class parent{
private Integer id;
#OnetoMany
#JoinColumn(name="parentId")
private List<child> children;
}
public class child{
private Integer id;
private Integer parentId;
}
findByIdAndchildType(Integer id, String type)
I want to see : parent(id) - > child (type)
But i can see parent(id) - > child(othertype), child(othertype1), child(type)
It sounds to me that you're trying to get a bi-directional relation. This is possible by adding the mapping to both sides of the relation.
For example, add a #ManyToOne mapping to the Child entity. Be aware that you should probably remove your parentId field since now you can access it by using child.getParent().getId().
#Entity
public class Child {
#Id
private Integer id;
#ManyToOne
#JoinColumn(name = "parentId")
private Parent parent;
// Remove parentId field
// Getters + Setters ...
}
NOTE: If you want to keep the parentId field, you'll have to choose which two of the mappings (getParentId() or getParent().getId()) you want to use for inserting and updating entities. The other field should have both insertable = false and updatable = false.
The next step is to change the #OneToMany mapping to use mappedBy:
#Entity
public class Parent {
#Id
private Integer id;
#OneToMany(mappedBy = "parent") // Change this
private List<Child> children;
// Getters + Setters ...
}
If you want to retrieve a specific child with its parent, you can now create a repository for Child entities:
public interface ChildRepository extends JpaRepository<Child, Integer> {
}
After that, you can get a specific child by using:
Optional<Child> child = repository.findById(123); // 123 is the ID of the child in this case
Optional<Parent> parent = child.map(Child::getParent);
With Spring boot 1.x that would be:
Child child = repository.findOne(123);
Parent parent = null;
if (child != null) {
parent = child.getParent();
}

Many to many association without join table

I'd like to apply JPA in the following (simplified) database:
NODE AUTHORITY
----- ----------
idNode int idAuthorities int
nameNode varchar(50) person varchar(255)
idAuthorities int rank int
PRIMARY KEY (idNode) PRIMARY KEY (idAuthorities, rank)
FOREIGN KEY (idAuthorites)
So one node can have multiple authorities and one authority can be referenced by multiple nodes.
And I wanted my classes to look like:
#Entity
#Table(name="NODE")
public class Node {
private Integer id;
private String nameNode;
private Set<Authority> authorities;
// ... getter and setter normaly annoted for "id" and "nameNode"
#ManyToMany
public Set<Authority> getAuthorities(){
return authorities;
}
// ... setter ...
}
#Entity
#Table(name="AUTHORITY")
public class Authority {
private AuthorityPK pk;
private String person;
privat Set<Node> nodes;
// ... getter and setter normaly annoted for "person"
#Id
public AuthorityPK getPk(){
return this.pk
}
// ... setter ...
#ManyToMany
public Set<Node> getNodes(){
return nodes;
}
// ... setter ...
}
#Embeddable
public class AuthorityPK implements Serializable {
private Integer idAuthorities;
private Integer rankAuthorities;
// override of equals and hashCode
}
But the annotation "#ManyToMany" seems to be usable only with "#JoinTable", which isn't usable (as far as I understand) in that case.
Does anyone know if there is a way arroud beside modifying the database?
JPA does not allow this as it requires foreign keys to reference the full primary key for identity purposes, and it might not work well with caching. If you can, I would recommend switching to a more traditional model using a relation table that uses the actual primary keys.
If your provider allows mapping partial pks (I believe Hibernate does), what you would do is make two 1:M mappings each using a JoinColumn instead of JoinTable, but mark the one on Node->Authority as insertable=false, updatable=false
For example, something like:
public class Node {
#Id
private Integer id;
private String nameNode;
#OneToMany
#JoinColumn(name = "idAuthorites", referencedColumnName = "idAuthorites", insertable=false, updatable=false)
private Set<Authority> authorities;
...
public class Authority {
#Id
private AuthorityPK pk;
private String person;
#OneToMany
#JoinColumn(name = "idAuthorites", referencedColumnName = "idAuthorites")
private Set<Node> nodes;
...

Hibernate failing by prepending fully qualified class name to property name on ManyToMany association

I'm trying to map two objects to each other using a ManyToMany association, but for some reason when I use the mappedBy property, hibernate seems to be getting confused about exactly what I am mapping. The only odd thing about my mapping here is that the association is not done on a primary key field in one of the entries (the field is unique though).
The tables are:
Sequence (
id NUMBER,
reference VARCHAR,
)
Project (
id NUMBER
)
Sequence_Project (
proj_id number references Project(id),
reference varchar references Sequence(reference)
)
The objects look like (annotations are on the getter, put them on fields to condense a bit):
class Sequence {
#Id
private int id;
private String reference;
#ManyToMany(mappedBy="sequences")
private List<Project> projects;
}
And the owning side:
class Project {
#Id
private int id;
#ManyToMany
#JoinTable(name="sequence_project",
joinColumns=#JoinColumn(name="id"),
inverseJoinColumns=#JoinColumn(name="reference",
referencedColumnName="reference"))
private List<Sequence> sequences;
}
This fails with a MappingException:
property-ref [_test_local_entities_Project_sequences] not found on entity [test.local.entities.Project]
It seems to weirdly prepend the fully qualified class name, divided by underscores. How can I avoid this from happening?
EDIT:
I played around with this a bit more. Changing the name of the mappedBy property throws a different exception, namely:
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: test.local.entities.Project.sequences
So the annotation is processing correctly, but somehow the property reference isn't correctly added to Hibernate's internal configuration.
I have done the same scenario proposed by your question. And, as expected, i get the same exception. Just as complementary task, i have done the same scenario but with one-to-many many-to-one by using a non-primary key as joined column such as reference. I get now
SecondaryTable JoinColumn cannot reference a non primary key
Well, can it be a bug ??? Well, yes (and your workaround works fine (+1)). If you want to use a non-primary key as primary key, you must make sure it is unique. Maybe it explains why Hibernate does not allow to use non-primary key as primary key (Unaware users can get unexpected behaviors).
If you want to use the same mapping, You can split your #ManyToMany relationship into #OneToMany-ManyToOne By using encapsulation, you do not need to worry about your joined class
Project
#Entity
public class Project implements Serializable {
#Id
#GeneratedValue
private Integer id;
#OneToMany(mappedBy="project")
private List<ProjectSequence> projectSequenceList = new ArrayList<ProjectSequence>();
#Transient
private List<Sequence> sequenceList = null;
// getters and setters
public void addSequence(Sequence sequence) {
projectSequenceList.add(new ProjectSequence(new ProjectSequence.ProjectSequenceId(id, sequence.getReference())));
}
public List<Sequence> getSequenceList() {
if(sequenceList != null)
return sequenceList;
sequenceList = new ArrayList<Sequence>();
for (ProjectSequence projectSequence : projectSequenceList)
sequenceList.add(projectSequence.getSequence());
return sequenceList;
}
}
Sequence
#Entity
public class Sequence implements Serializable {
#Id
private Integer id;
private String reference;
#OneToMany(mappedBy="sequence")
private List<ProjectSequence> projectSequenceList = new ArrayList<ProjectSequence>();
#Transient
private List<Project> projectList = null;
// getters and setters
public void addProject(Project project) {
projectSequenceList.add(new ProjectSequence(new ProjectSequence.ProjectSequenceId(project.getId(), reference)));
}
public List<Project> getProjectList() {
if(projectList != null)
return projectList;
projectList = new ArrayList<Project>();
for (ProjectSequence projectSequence : projectSequenceList)
projectList.add(projectSequence.getProject());
return projectList;
}
}
ProjectSequence
#Entity
public class ProjectSequence {
#EmbeddedId
private ProjectSequenceId projectSequenceId;
#ManyToOne
#JoinColumn(name="ID", insertable=false, updatable=false)
private Project project;
#ManyToOne
#JoinColumn(name="REFERENCE", referencedColumnName="REFERENCE", insertable=false, updatable=false)
private Sequence sequence;
public ProjectSequence() {}
public ProjectSequence(ProjectSequenceId projectSequenceId) {
this.projectSequenceId = projectSequenceId;
}
// getters and setters
#Embeddable
public static class ProjectSequenceId implements Serializable {
#Column(name="ID", updatable=false)
private Integer projectId;
#Column(name="REFERENCE", updatable=false)
private String reference;
public ProjectSequenceId() {}
public ProjectSequenceId(Integer projectId, String reference) {
this.projectId = projectId;
this.reference = reference;
}
#Override
public boolean equals(Object o) {
if (!(o instanceof ProjectSequenceId))
return false;
final ProjectSequenceId other = (ProjectSequenceId) o;
return new EqualsBuilder().append(getProjectId(), other.getProjectId())
.append(getReference(), other.getReference())
.isEquals();
}
#Override
public int hashCode() {
return new HashCodeBuilder().append(getProjectId())
.append(getReference())
.hashCode();
}
}
}
I finally figured it out, more or less. I think this is basically a hibernate bug.
edit: I tried to fix it by changing the owning side of the association:
class Sequence {
#Id
private int id;
private String reference;
#ManyToMany
#JoinTable(name="sequence_project",
inverseJoinColumns=#JoinColumn(name="id"),
joinColumns=#JoinColumn(name="reference",
referencedColumnName="reference"))
private List<Project> projects;
}
class Project {
#Id
private int id;
#ManyToMany(mappedBy="projects")
private List<Sequence> sequences;
}
This worked but caused problems elsewhere (see comment). So I gave up and modeled the association as an entity with many-to-one associations in Sequence and Project. I think this is at the very least a documentation/fault handling bug (the exception isn't very pertinent, and the failure mode is just wrong) and will try to report it to the Hibernate devs.
IMHO what you are trying to achieve is not possible with JPA/Hibernate annotations. Unfortunately, the APIDoc of JoinTable is a bit unclear here, but all the examples I found use primary keys when mapping join tables.
We had the same issue like you in a project where we also could not change the legacy database schema. The only viable option there was to dump Hibernate and use MyBatis (http://www.mybatis.org) where you have the full flexibility of native SQL to express more complex join conditions.
I run into this problem a dozen times now and the only workaround i found is doing the configuration of the #JoinTable twice with swapped columns on the other side of the relation:
class Sequence {
#Id
private int id;
private String reference;
#ManyToMany
#JoinTable(
name = "sequence_project",
joinColumns = #JoinColumn(name="reference", referencedColumnName="reference"),
inverseJoinColumns = #JoinColumn(name="id")
)
private List<Project> projects;
}
class Project {
#Id
private int id;
#ManyToMany
#JoinTable(
name = "sequence_project",
joinColumns = #JoinColumn(name="id"),
inverseJoinColumns = #JoinColumn(name="reference", referencedColumnName="reference")
)
private List<Sequence> sequences;
}
I did not yet tried it with a column different from the primary key.

Categories

Resources