Hibernate mapping: OneToMany and OneToOne on child object property - java

Here is parent class Enterprise. It has employers and one of them is president of enterprise.
#Entity
class Enterprise
{
// fields
#OneToMany
public List<Employee> getEmployers()
// implementation
#OneToOne
public Employee getPresident()
// implementation
}
Here is child Employee class. It has only info about Enterprise where he works. But question is what association should I use?
#Entity
class Employee
{
// fields
// what association should I use?
public Enterprise getEnterprise()
// implementation
}

Given that you've defined the Enterprise->Employers association with #OneToMany, which means that an Employer belongs to only one Enterprise, you should be using #ManyToOne, meaning that every Employer belongs to max. 1 Enterprise, but an Enterprise can reference many Employers.
You can define the association specifics (join columns, etc) in one of the sides only, using the mapped-by attribute in the annotation:
#Entity
class Enterprise
{
#OneToMany(mapped-by="enterprise")
public List<Employee> getEmployers()
// implementation
#OneToOne
public Employee getPresident()
// implementation
}
#Entity
class Employee
{
#ManyToOne
#JoinTable ( name="Enterprise", joinColumns={ #JoinColumn(name="ENT_ID", referencedColumnName="ENT_ID") }
public Enterprise getEnterprise()
// implementation
}
In case an Employer could be president of a different Enterprise in which he is employed (seems unlikely, unless one can be president of an enterprise without being employed by it), and in case you needed to access the Enterprise of which the Employer is president from the Employer entity, you would need to add another association, ideally with #OneToOne (you would encounter problems, because #OneToOne relations require both entities to have the same #Id class). In this case I would annotate the getPresidedEnterprise() method on Employer with #ManyToOne for practical reasons.

Use #ManyToOne annotation. It is the opposite side of a one-to-many relation. It says an employee can have one enterprise, but an enterprise can have many employees.

You should have two properties on the Employee class. Employee table should have a reference to an enterprise, ad enterprise should have a reference to an employee-president.
(you could also probably subclass an Employee based on the isPresident flag column, but I don't have experience with that)
#Entity
class Enterprise
{
// fields
#OneToMany(mappedBy="enterprise")
public List<Employee> getEmployees(){}
#OneToOne
#JoinColumn(name="PRESIDENT_ID")
public Employee getPresident(){}
}
#Entity
class Employee
{
// fields
#ManyToOne
#JoinColumn(name="ENTERPRISE_ID")
public Enterprise getEnterprise(){}
#OneToOne(mappedBy="President")
public Enterprise getMyEnterprise(){}
}

Related

Hibernate: Allow only entities with given attribut to a OneToMany association

Is it possible to check in a #OneToManyor #ManyToMany association if the many side has a given attribut value?
For example, students visiting a lecture:
#Entity
class Lecture implements Serializable {
#Id
Integer id;
#OneToMany
Set<Student> student;
}
#Entity
class Student implements Serializable {
#Id
Integer id;
Boolean isFemale;
}
Can I enforce with a "magic" annotation that only female students are allowed to visit the lecture?
Your #OneToMany annotation will execute a SELECT statement. You might be able to filter the result with vendor proprietary annotations like #Where (https://forum.hibernate.org/viewtopic.php?f=1&t=1026210&view=next).
But it seems that you want to restrict the INSERT case. You might just use a Java bean validator. So, if your Student class had the reverse #ManyToOne attribute lecture, then you could create a validator which rejects new student objects, which are linked to a lecture AND are female. (thus implementing your desired discrimination) (see bean validation: https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/validator-customconstraints.html#section-class-level-constraints)
But you might have guessed yourself that your constraint is not a real database constraint. It's not possible with SQL, so don't expect it to be possible with JPA.

JPA - What Annotation to use for a base class

I am a relative newbie to JPA, and I've read books (Java Persistence with Hibernate, Pro JPA 2 - Mastering the Java Persistence API), done google searches, but I have not been able to come up with a solution to the following situation.
I have a base class called History, that has all the persistent information needed to store the class's data members to a database. However, the class has an abstract method that needs to be overridden in derived classes. The derived classes do not have any persistence information of their own. They exist solely for two reasons:
to uniquely implement the abstract method defined in the base
History class
to persist the data to it's own table
The code below should make this clear.
#??? What annotation should I use here?
public abstract class History
{
#Id #GeneratedValue
private int id; // primary key
#Lob #Column(columnDefinition="mediumtext", length=65535)
protected String string; // string containing history
public abstract String foo();
}
#Entity
#Table(name="derived_history_1")
public class Derived1 extends History
{
public String foo()
{
return "Hello";
}
}
#Entity
#Table(name="derived_history_2")
public class Derived2 extends History
{
public String foo()
{
return "World";
}
}
I didn't think that #Inheritance(strategy=InheritanceType.JOINED) made sense, because nothing is being inherited in the derived classes that needs to be made persistent.
I tried #MappedSuperclass, but then the CascadeType.PERSIST and CascadeType.MERGE did not work when the derived classes were data members of another class. For example,
#Entity
#Table(name="part")
public class Part
{
...
#OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, optional=false, fetch=FetchType.LAZY)
#JoinColumn(name="history_id") // foreign key into history_part table
protected Derived1 history;
}
So I couldn't find anything that worked and fit my situation.
I hope I'm explaining this well enough that people can understand.
Does anyone know how to do this that's not a complete and total hack :-)
Thanks for any help.
Following annotations should work:
#Entity
#Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
Inheritance type TABLE_PER_CLASS makes inheriting entities to create own table and you also need to tell that History is also an Entity even it does not have table of its own.

Is correct to use #OneToMany with #OneToOne?

I have a class Portfolio where the debts that the debtors have with different banks are kept. Therefore, a portfolio has a list of Debt objects and the annotation is #OneToMany.
This is Portfolio:
    
#Entity
public class Portfolio extends PersistentEntity {
#OneToMany
private List<Debt> debts;
/* Getters and setters */
}
And the class Debt:
#Entity
public class Debt extends PersistentEntity {
#OneToOne
private Portfolio portfolio;
/* Getters and setters */
}
My question is what annotation to use in the Debt class. I understand it is #OneToOne because a debt belongs to a particular portfolio, but I was advised to use #ManyToOne. What I understand from this annotation is that a debt can be referenced by different portfolios. Is this correct?
You should use annotation #ManyToOne.
In your case, as you said Portfolio has a list of Debt objects and the annotation is #OneToMany.
On the other hand, each Debt can belong ONLY ONE Portfolio, so you should use annotation #ManyToOne
Also, see these links:
Hibernate mapping: OneToMany and OneToOne on child object property
When to use, not to use, OneToOne and ManyToOne
Difference Between One-to-Many, Many-to-One and Many-to-Many?
https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-one-1.html

Classes Relationships with JPA

I have a set of Java classes with the following UML diagram:
public class Invoice {
#Id
private long id;
...
}
public class InvoiceDetail {
#Id
private long id;
...
private String productName;
private int quantity;
private double price;
}
My purpose is using JPA annotations to establish the different relationships between them. There is a composition relationship between Invoice and InvoiceDetail, which is resolved using #Embedded and #Embeddable annotations for Invoice and InvoiceDetail respectively. However, a problem appears by establishing the relationships between InvoiceDetail, Class3 and Class4. In these relationships InvoiceDetail must be annotated as #Entity. However, when a class is annotated at the same time as #Entity and #Embeddable, the corresponding server will throw a runtime error during the deployment.
Basing on the information of this website, I have written the following possible solution:
#Entity
public class Invoice {
#Id
private long id;
...
#ElementCollection
#CollectionTable(name="INVOICEDETAIL", joinColumns=#JoinColumn(name="INVOICE_ID"))
private List<InvoiceDetail> invoiceDetails;
...
}
Would be this right in order to resolve my problem?
Thanks in advance.
Although without knowing what the classes really are it is hard to tell, I suppose that you have a design problem. The composition between Class1 and Class2 says that any Class2 instance only exists within the lifecycle of a corresponding Class1 instance. But on the other hand you have Class3 instances and Class4 instances which can / must have a relationship to a Class2 instance.
What I'm trying to say is that from my point of view the relationship between Class1 and Class2 should be a simple association and not a composition. Following this path Class2 would be an Entity in JPA and then you should have your problem solved.
I usually use #Embeddable for classes whose instances never exist by themselfes and #Entity for any class whose instances can exist without other instances. An address for example could be implemented either way but not on the same system. Address would be #Embeddable if I don't want to link addresses but it had to be #Entity if I want to make sure the same address isn't saved in more than one row.
[edit: added after classes 1 and 2 were renamed to Invoice and InvoiceDetails]
Having a composition between Invoice and InvoiceDetails makes perfect sense. But I still think you should avoid the need of double personality for InvoiceDetails. I can think of two solutions (both refactorings):
If you prefer having InvoiceDetails as #Embeddable you could change the associations of Class3 and Class4 to Invoice instead of InvoiceDetails. InvoiceDetails would still be traversable via the Invoice object.
If you prefer keeping the associations as is you could declare InvoiceDetails to be an entity. You could still achieve your composition with a cascading delete (see javax.persistence.CascadeType). As it seems that InvoiceDetails already has it's own table, this probably is the better option.
I checked my JPA applications and haven't found any occurence of the same class being #Entity and #Embeddable. Honestly, I doubt if this is possible at all because the official javadoc of #Embeddable says:
Specifies a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity.
As #Entity has it's own identity, you would try to declare the same object having two identities - and this can't work.
[/edit]
[edit2: adding code for solution proposal #2]
This code should work with some assumptions (see below). This is the implementation of bi-directional navigation for a 1:n-relationship.
#Entity
public class Invoice {
#Id
private long id;
#OneToMany(mappedBy="invoice", cascade = CascadeType.ALL)
private List<InvoiceDetail> details;
}
#Entity
public class InvoiceDetails {
#Id
private long id;
#ManyToOne
#JoinColumn(name="invoice_id")
private Invoice invoice;
}
Assumptions: Tables are named like the entities, the foreign key column for invoice_details table is named "invoice_id" and both tables have a primary key column named "id". Note that the mappedBy-value "invoice" refers to the entity field while the name-value "invoice_id" refers to the database table.
Be cautious when deleting an Invoice object whose InvoiceDetails still are referenced by your Class3 or Class4 instances - you have to release these references first.
For information about JPA refer to these resources:
The Java EE 7 Tutorial: Persistence
Wikibooks: Java Persistence
Javadoc of Package javax.persistence
[/edit]

Hibernate ManyToMany and superclass mapping problem

I need to create a relation in Hibernate, linking three tables: Survey, User and Group.
The Survey can be visible to a User or to a Group, and a Group is form of several Users.
My idea was to create a superclass for User and Group, and create a ManyToMany relationship between that superclass and Survey.
My problem is that Group, is not map to a table, but to a view, so I can't split the fields of Group among several tables -which would happen if I created a common superclass-.
I thought about creating a common interface, but mapping to them is not allowed.
I will probably end up going for a two relations solution (Survey-User and Survey-Group), but I don't like too much that approach.
I thought as well about creating a table that would look like:
Survey Id | ElementId | Type
ElementId would be the Group or UserId, and the type... the type of it.
Does anyone know how to achieve it using hibernate annotations? Any other ideas?
Thanks a lot
I posted a very similar answer yesterday. To summarize, you can't use a mapped superclass because a mapped superclass is not an entity and can't be part of an association (which is what you want) but you can use an abstract Entity with a TABLE_PER_CLASS inheritance strategy to obtain a similar result.
Something like this (not tested):
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity {
#Id #GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
#ManyToMany(mappedBy="entities")
private Set<Survey> surveys = new HashSet<Survey>();
...
}
#Entity
public class User extends AbstractEntity {
...
}
#Entity
public class Group extends AbstractEntity {
...
}
#Entity
public class Survey {
#Id #GeneratedValue
private Long id;
#ManyToMany
private Set<AbstractEntity> entities = new HashSet<AbstractEntity>();
...
}
References
Annotations, inheritance and interfaces
using MappedSuperclass in relation one to many
Polymorphic association to a MappedSuperclass throws exception
You can use the table per concrete class inheritance strategy, hibernate will replicate all properties for each subclass, this will work with a view.
I would also suggest the composite pattern for users/groups (which is close to your first option).
http://en.wikipedia.org/wiki/Composite_pattern
This is possible. Such an 'inherited properties' approach can be achieved by defining the superclass as a MappedSuperclass.
EDIT:
There is also some alternatives listed in section 2.2.4 in the hibernate annotations reference doc, section 2.2.4.4 covers MappedSuperclass.

Categories