JPA relation OneToMany - jpa

I was thinking that I understand the relations. But now.. Im not sure. I have a problem to add correct annotations.
I have classes Members and Relations.
In Relations
private GT_Member Mother;
private GT_Member Father;
private List<GT_Member> children;
One Member can be in several Relations as Mother or Father (reference to his sex) but he can be only in One relation as child.
I thought annotate Mother and Father as #OneToMany.
But I'm not sure if I can annotate List as #OneToOne ??

This seems like a problem in modeling the correct entity-relationship model for your database schema and visualizing your ORM (Object Relationship Model).
Rather than starting with classes Members and Relations, please first see what are the dominant data-entities in your system. And how would they be related to each other.
Personally I do not think Relation would be a good JPA entity.
Member looks more like a good entity and could embody the relations
Assuming one Father and one Mother, One to Many seems wrong but as a father or mother can have many children, the correct annotation should be #ManyToOne.
Children is definitely OneToMany, and yes you can annotate the List children as #OneToMany.
Member could have the following properties:
#Entity
public class Member implements Serializable{
#ManyToOne
private Member mother;
#ManyToOne
private Member father;
#OnetoMany
private List<Member> children;
}
This solves both your use cases and in this simple example Relation class is not needed.
hope this helps.

Employment of Relation entity is because I want to save information about status of relation. Donc I will store information about all married etc. Entity relation has other fields like type (neutral, married, fiance etc...).

Related

Database not written to when relationship edited from the inverse side on a ManyToMany relationship

JPA Provider is EclipseLink. Having the following entities:
#Entity
class Symptom {
#ManyToMany(mappedBy = "symptoms")
private Set<Disorder> disorders;
}
#Entity
class Disorder {
#ManyToMany
#JoinTable(name = "disorder_symptoms")
private Set<Symptom> symptoms;
}
One symptom can be found on many disorders and many disorders can have many symptoms. Symptom is defined as the inverse (non-owning) side of the relationship, but this being a bidirectional relationship, it shouldn't matter.
When i add a new symptom to Disorder, database table disorder_symptoms is updated as expected. But when i add a disorder to Symptom, the table is not populated. Tried with cascade attribute set for either side, still no result.
Is there something wrong i'm doing here?

How to map a JPA bidirectional relationship involving an abstract class?

Assuming the following simple schema:
Entity table - attributes common to all entities
entityId, timeCreated,...
Comments table - some entities have a collection of these
entityId, commentId, commentText,....
Person table.
pensonId (entityId), firstName, lastName,...
And the following Java inheritance structure:
BaseEntity - abstract - entity, join inheritance with a discriminator
CommentedEntity - abstract - introduces `comments` collection - mapping tbd
Person - concrete - entity - mapping trivial
How would we map bidirectional relationship between CommentedEntity and Comment? The code below is my best interpretation of examples I have found.
CommentedEntity
#OneToMany(mappedBy="owner", fetch=FetchType.EAGER)
private List<Comment> comments = new ArrayList<>();
Comment
#ManyToOne(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name="entityId", referencedColumnName="entityId")
private CommentedEntity owner;
Since CommentedEntity is an abstract class, this doesn't work.
owner references an unknown entity.
Making CommentedEntity an entity, would require giving it an id, so I don't think that makes
sense.
Since multiple concrete entities will have comments
collection, concrete entity name cannot be used in mapping.
How would we then bidirectionally map a person.commentList property?
If Person extends CommentedEntity then you wouldn't need an CommentedEntity owner for the Person entity because Person is part of aCommentedEntity`. In other words, there is no need for an owner field because a Person is a CommentedEntity.

mapping list of Person in another person

I have a person Entity and two list of persons in it, that i implemented this way (thanks to this post : Hibernate many-to-many association with the same entity) :
#ManyToMany
#JoinTable(name="tbl_friends",
joinColumns=#JoinColumn(name="personId"),
inverseJoinColumns=#JoinColumn(name="friendId")
)
private List<User> friends;
#ManyToMany
#JoinTable(name="tbl_friends",
joinColumns=#JoinColumn(name="friendId"),
inverseJoinColumns=#JoinColumn(name="personId")
)
private List<User> friendOf;
But, with the #ManyToMany annotation the Cascadings (MERGE,DELETE,etc..) doesn't work.
Is there a way to achieve the same mapping but with enabling Cascadings ?
Cascadings do work with many-to-many associations. But most of the time, there shouldn't be any cascade set on a many-to-many association: since a friend is a friend of many persons, you can't, for example, delete all John's friends (Paul and Matt) when you delete John. Indeed, many other people (Jack, Sarah) also have Paul and Matt as friends, and it would thus lead to a constraint violation.
The problem with your code is that the mapping is wrong. You have a single, bidirectional, many-to-many association here, but you mapped it as two unidirectional many-to-many associations, using the same join table.
In a bidirectional association, one side must be the inverse side. If you choose friendOf as the inverse side, it should thus be mapped as
#ManyToMany(mappedBy = "friends")
private List<User> friendOf;

JPA Simple ForeignKey relationship

Is it possible to create a basic FK relationship in JPA without involving the full entity target object?
As an example, imagine I have an entity:
#Entity(name = "Mechanic")
public class Mechanic {
#Id
private Long id;
//...
and a Car that I want to reference a Mechanic.id:
#Entity(name = "Car")
public class Car {
//...
#NotNull
private Long mechanic_id;
From an Object perspective, this would be a unidirectional, one to one relationship with the Car requiring a Mechanic.id and the Mechanic not needing any back reference to Car.
All I want out of this is to store the Mechanic.id ONLY. For the purposes of this question it is not useful to have a #OneToOne (or #OneToMany etc) relationship with the entity reference, I'm explicitly trying to avoid that but still retain the underlying integrity that a FK will provide.
JPA 2 and I'm using EclipseLink.

JPA oredered lists #OrderedBy and #OrederColumn

I have a question about the #OrderBy and #OrderColumn of the JPA spec.
The problem is that I will have an entity that maintains a list of elemets. Each element will appear in several lists. As I understand the #OrderBy and #OrderColumn annotations, you cannot have the same element appear in different positions in many ordered lists. I just can't believe this.
Can someone explain these annotations a bit? The difference between these two annotations? Specifically how I could do soemthing like this:
#Entity
public class Class{
#ManyToMany
private List<Students> studentsInRankedOrder;
// this list should be ordered by class rank.
// but a student who is ranked 1 in one class will most likely not be ranked 1
// in another course.
}
#Entity
public class Student{
#ManyToMany
private List<Class> enrolledIn;
}
Thanks a lot.
http://openjpa.apache.org/builds/2.1.1/apache-openjpa/docs/manual.html#ref_guide_mapping_jpa_coll_order
I understand now. "Order columns are always in the container table." This makes much more sense to me.