JPA, inheritance JOINED, multiple ID's - java

I have a little JPA question.
Assume you have such tables: (structure is fixed)
PERSON
--
ID,
DEPARTMENT_ID
...
SPECIALWORKER
------
ID, PERSON_ID, SPECIALDEPARTMENT_ID
...
DEPARTMENT
-------
ID,
...
SPECIALDEPARTMENT
-------
ID,
...
In Java I would build it with a simple hierarchy: SpecialWorker extends Person and SpecialDepartment extends Department. We will have "simple" Persons as well as "simple" Departments.
In JPA I try to build that scenario with a JOINED_Table inheritance, but I can't get it working. Any ideas?
edit
the coding, i hope i missed nothing.
I got a integrity exception when i try to insert a specialworker.
When i insert a specialworker jpa has to set the fk to departement (baseclass person) as well as the fk to the special departement (from the concrete current class)
#Entity
#Table(name = "DEPARTEMENT")
#Access(AccessType.FIELD)
#Inheritance(strategy = InheritanceType.JOINED)
public class Departement
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Integer id;
...
}
#Entity
#Table(name = "SPECIALDEPARTEMENT")
#Access(AccessType.FIELD)
#PrimaryKeyJoinColumn(name = "ID_DEPARTEMENT", referencedColumnName = "ID")
public class SpecialDepartement
extends Departement
implements Serializable
{
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", updatable = false, insertable = false)
private Integer id01;
#OneToMany(mappedBy = "specialDepartement", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SpecialWorker> workers;
...
}
#Entity
#Table(name = "PERSON")
#Access(AccessType.FIELD)
#Inheritance(strategy = InheritanceType.JOINED)
public class Person
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
#Max(19)
private Long id;
#ManyToOne
#JoinColumn(name = "ID_DEPARTEMENT", referencedColumnName = "ID", nullable = false)
private Departement departement;
...
}
#Entity
#Table(name = "SWORKER")
#Access(AccessType.FIELD)
#PrimaryKeyJoinColumn(name = "ID_PERSON", referencedColumnName = "ID")
public class SpecialWorker
extends Person
{
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", updatable = false, insertable = false)
private Integer id01;
#ManyToOne
#JoinColumn(name = "ID_SPECIALDEPARTEMENT", referencedColumnName = "ID", nullable = false)
private SpecialDepartement specialdepartement;

Related

JPA OneToOne association without foreign key not working

I am using Hibernate/JPA, and have 3 tables:
Contact
pk contact_id
fk member_id
Employee
pk employee_id
fk member_id
Members
pk member_id
I have:
#Entity(name = "Contact" )
public class Contact implements Serializable {
#Id
#SequenceGenerator(allocationSize = 1, name = "contact_sequence", sequenceName = "contact_ids")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contact_sequence")
private java.lang.Long contact_id;
#OneToOne(cascade = CascadeType.ALL, targetEntity = Members.class)
#JoinColumn(name = "member_id")
private Members member;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "member", referencedColumnName = "member")
private Employee employee;
and
#Entity(name = "Employee")
public class Employee implements Serializable {
#Id
#SequenceGenerator(allocationSize = 1, name = "employee_sequence", sequenceName = "employee_ids")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_sequence")
private java.lang.Long employee_id;
and
#Entity(name = "Members")
#Table(name = "Members")
public class Members implements Serializable {
#Id
#SequenceGenerator(allocationSize = 1, name = "members_sequence", sequenceName = "member_ids")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "members_sequence")
private java.lang.Long member_id;
My problem is with the Contact entity private Employee employee;. When I start my Jboss server:
StartException in service jboss.persistenceunit.
Question
How do I annotate the private Employee employee; on the Contact entity?
Thank you
You are almost properly defining the private Members member;, so is there a reason for not doing the same for private Employee employee; ?
Like :
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "employee", referencedColumnName = "employee_id")
private Employee employee;
Also a simple link, just to cross-check the overall #JoinColumn functionality
===EDIT===
As it looks there is indeed a data structure issue as from the first place the relation is ManyToOne,
If you could try the below example :
//Employee
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "contact_id", referencedColumnName = "contact_id")
private Contact contact;
//Contact
#OneToMany(fetch = FetchType.LAZY, mappedBy = "employee_id")
private List<Employee> employees = new ArrayList<>();
Then by selecting the contact.getEmployees() should do the trick

Problems with identifiing entity in another entity using Hibernate

Here is the code:
#Entity
#Table(name = "students")
public class BotUser {
...
#Id
#Column(name = "id", updatable = false)
private int id;
#OneToOne
private Equipment equipment;
...
}
#Entity
#Table(name = "students")
public class Equipment {
...
#Id
#Column(name = "id")
private int id;
...
}
When Hibernate is fetching equipment data from PostrgeSQL table, he requests "equipment_id" field, not "id". How to solve this problem?
I must use #JoinColumn annotation. And to be sure I added CascadeType.ALL
#Entity
#Table(name = "students")
public class BotUser {
...
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "id")
private Equipment equipment;
...
}
The same problem: AnnotationException Referenced property not a (One|Many)ToOne

MappedBy reference an unknown target entity

I design a system of tables in the database for the film service. So far I have designed them in this way.
#Entity
#Table(name = "movies")
#Data
public class MovieEntity {
#Id
#Column(unique = true, updatable = false)
#GeneratedValue
private Long id;
#OneToMany(mappedBy = "movie", cascade = CascadeType.ALL)
private Set<MovieDescription> description;
}
#Entity
#Table(name = "movies_info")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type")
public abstract class MovieInfo {
#Id
#Column(unique = true, updatable = false)
#GeneratedValue
private Long id;
#ManyToOne
public MovieEntity movie;
}
#Entity
#DiscriminatorValue(value = EditType.Values.DESCRIPTION)
public class MovieDescription extends MovieInfo {
private String description;
private String language;
}
When compiling, it sends me a mistake
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.core.jpa.entity.MovieDescription.movie in com.core.jpa.entity.MovieEntity.description
Something related to MovieEnity mapping, but I don't know what it is all about.
use targetEntity attribute to target the super class field that you want to map with.
#Entity
#Table(name = "movies")
#Data
public class MovieEntity {
#Id
#Column(unique = true, updatable = false)
#GeneratedValue
private Long id;
#OneToMany(mappedBy = "movie", cascade = CascadeType.ALL, targetEntity= MovieInfo.class)
private Set<MovieDescription> description;
}
More detail: one to many mapping to a property of superclass

Hibernate mapping many columns to one table

How to configure mapping? I want to table "Remittance" were two columns referring to the table "Expense"
Remittance
#Entity
#Table(name = "REMITTANCE")
public class Remittance implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "REMITTANCE_ID")
private Long id;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "remittances", cascade = CascadeType.ALL)
private Expense from;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "remittances", cascade = CascadeType.ALL)
private Expense to;
}
Expense
#Entity
#Table(name = "EXPENSE")
public class Expense implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "EXPENSE_ID")
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "REMITTANCE_ID")
private Remittance remittances;
}
I made a mistake. I have corrected
Remittance
#Entity
#Table(name = "REMITTANCE")
public class Remittance implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "REMITTANCE_ID")
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "EXPENSE_ID")
private Expense from;
#ManyToOne(optional = false)
#JoinColumn(name = "EXPENSE_ID")
private Expense to;
}
Expense
#Entity
#Table(name = "EXPENSE")
public class Expense implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "EXPENSE_ID")
private Long id;
}
but now out
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: ru.make.alex.web.model.revenue.Remittance column: EXPENSE_ID (should be mapped with insert="false" update="false")

JPA Many-to-Many Join table entity with compound key "null id generated "

This are my entities:
public class Account extends AbstractEntity<Long> {
#Id
#SequenceGenerator(name = "accountSequence", sequenceName = "SQ_ACCOUNTS", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
#Column(name = "ACC_ID", nullable = false)
private Long id;
...
}
public class Integration extends AbstractEntity<Long> {
#Id
#SequenceGenerator(name = "integrationSequence", sequenceName="SQ_INTEGRATIONS", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "integrationSequence")
#Column(name = "INT_ID", nullable = false)
private Long id;
...
public void addIntegration(Integration integration) {
IntegrationAccount association = new IntegrationAccount();
// This does not help
//association.setIntAccountsPK(new IntAccountsPK(integration.getId(), this.getId()));
association.setAccount(this);
association.setIntegration(integration);
this.integrationAccounts.add(association);
integration.getIntAccountsCollection().add(association);
}
}
And this is entity for join table
#Entity
#Table(name = "INT_ACCOUNTS")
public class IntegrationAccount {
#EmbeddedId
protected IntAccountsPK intAccountsPK;
#JoinColumn(name = "ACC_ID", referencedColumnName = "ACC_ID", insertable = false, updatable = false)
#ManyToOne
private Account account;
#JoinColumn(name = "INT_ID", referencedColumnName = "INT_ID", insertable = false, updatable = false)
#ManyToOne
private Integration integration;
...
}
#Embeddable
public class IntAccountsPK implements Serializable {
#Column(name = "INT_ID", nullable = false)
private Long intId;
#Column(name = "ACC_ID", nullable = false)
private Long accId;
...
}
And when i do:
account.addIntegrations(integrations.getTarget());
account.setCustomer(customer);
accountService.save(account);
I got this in my log
Caused by: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.dhl.dcc.domain.IntegrationAccount
I dont have many knowledge about this kind of mapping, can you please tell me how to improve this mapping (entity for join table have to be preserved) and how to save account with related integrations? Thanks.
I know this question has already been marked as solved but I disagree with the accepted answer. This answer modifies the datamodel by adding a useless column (the new id) in the table INT_ACCOUNTS. There is another way to solve this problem in Hibernate without modifying the datamodel :
#Entity
#Table(name = "INT_ACCOUNTS")
public class IntegrationAccount implements Serializable {
#Id
#ManyToOne
#JoinColumn(name = "INT_ID_FK")
private Integration integration;
#Id
#ManyToOne
#JoinColumn(name = "ACC_ID_FK")
private Account account;
}
#Entity
#Table(name = "INTEGRATIONS")
public class Integration {
#Id
#SequenceGenerator(name = "integrationSequence", sequenceName = "SQ_INTEGRATIONS", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "integrationSequence")
#Column(name = "INT_ID")
private Long id;
}
#Entity
#Table(name = "ACCOUNTS")
public class Account {
#Id
#SequenceGenerator(name = "accountSequence", sequenceName = "SQ_ACCOUNTS", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
#Column(name = "ACC_ID")
private Long id;
}
You could create a ID field for your IntegrationAccount and then create a unique constraint for your two fields.
#Entity
#Table(name = "INT_ACCOUNTS",
uniqueConstraints=#UniqueConstraint(columnNames={"ACC_ID", "INT_ID"}))
public class IntegrationAccount {
#Id
private Long id;
#JoinColumn(name = "ACC_ID", referencedColumnName = "ACC_ID", insertable = false, updatable = false)
#ManyToOne
private Account account;
#JoinColumn(name = "INT_ID", referencedColumnName = "INT_ID", insertable = false, updatable = false)
#ManyToOne
private Integration integration;
...
}
Works like a charm!

Categories