openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mansour Al Akeel <mansour.alak...@gmail.com>
Subject Re: How to map an ID of a foreign key as a field
Date Tue, 24 Jun 2014 13:26:30 GMT
John,

I highly appreciate your help. I will try to give you the complete picture.
In an modular Java application based on OSGI, each module contains the
entities and services/daos. For each module that uses JPA they have
persistence unit.
Other modules may not even use ORM and may use direct JDBC calls. In this
case passing the foriegn key as an ID is the first option.

Now, building on the Employee, Department example, if we have two modules,
one that provides services related to Departments, and the the second for
Employees.
The Employees module might be JPA based. The Departments modules based on
JDBC calls. In other words, while Employee's Entity is persistence capable,
Departments Entity is not.
Let's say, in the Employee's Module, we want to insert new employee with
the department or move an employee from Department to another. Having the
foreign key as a value, makes things easier.


The second option is to modify the service/dao layer to accept the entity
with a foreign key, and then retrieve it. However, this is not very not
desired, because we are planning to use a generic-dao implementation.
For example in EmployeeDao:

create(Employee employee , String depId){

Department dep = em.getReference(Department.class , depId);

employee.setDepartment(dep);

em.persist(employee);

return employee;
}

This is an option, but I will try to avoid it because it breaks the
generic-dao pattern.
Additionally we need to create the tables from classes and maintain the
constraints.

I googled a lot for a technique that allow me to do this, but no luck. I
hope this makes everything clear.

Thank you.




On Tue, Jun 24, 2014 at 2:23 AM, Boblitz John <john.boblitz@bertschi.com>
wrote:

> > -----Original Message-----
> > From: Mansour Al Akeel [mailto:mansour.alakeel@gmail.com]
> > Sent: Montag, 23. Juni 2014 22:30
> > To: users
> > Subject: Re: How to map an ID of a foreign key as a field
> >
> >  If this can not be done in JPA I will appreciate it if someone can let
> me know.
> >
> >
> >
> >
> > On Mon, Jun 23, 2014 at 9:24 AM, Mansour Al Akeel
> > <mansour.alakeel@gmail.com
> > > wrote:
> >
> > > John,
> > >
> > > the classes are not in the same persistence unit. So even getReference
> > > will not work.
> > >
> > > Thank you.
> > >
> > >
> > >
> > > On Mon, Jun 23, 2014 at 7:49 AM, Boblitz John
> > > <john.boblitz@bertschi.com>
> > > wrote:
> > >
> > >> > -----Original Message-----
> > >> > From: Mansour Al Akeel [mailto:mansour.alakeel@gmail.com]
> > >> > Sent: Montag, 23. Juni 2014 10:23
> > >> > To: users
> > >> > Subject: How to map an ID of a foreign key as a field
> > >> >
> > >> > I am looking to map a field in the owner entity to the ID of the
> > >> > target
> > >> entity.
> > >> > For example, taking the Employee -> Department mapping:
> > >> >
> > >> > class Employee {
> > >> >
> > >> > private String name ;
> > >> >
> > >> > private String depId ;
> > >> >
> > >> > private Department department ;
> > >> >
> > >> >     @ManyToOne(fetch = FetchType.EAGER, optional = false)
> > >> >     @JoinColumn(name = "dep_id", referencedColumnName = "id",
> > >> > insertable = false, updatable = false) public Department
> > >> > getDepartment(){ return this.department ; }
> > >> >
> > >> >
> > >> > public void setDepId(String depId){ this.depId = depId; } ...
> > >> >
> > >> > }
> > >> >
> > >> > My Question is, how can I set the Deparment by setting only its
> > >> > dep_id
> > >> in the
> > >> > employee object, and keep the foreign key constraints when the
> > >> > tables
> > >> are
> > >> > created ??
> > >> >
> > >> > For example:
> > >> >
> > >> > Employee emp = new Employee() ;
> > >> > emp.setId("SOME_ID_1") ;
> > >> > emp.setDepId("FINANCE");
> > >> >
> > >> > em.persist(emp);
> > >> >
> > >> > and if there's no FINANCE record in the department table, then we
> > >> > have
> > >> an
> > >> > constraints violation.
> > >> >
> > >> >  At the same time I don't want to have setters for Department
> Entity.
> > >> Only its
> > >> > ID.
> > >> >
> > >> > Thank you.
> > >>
> > >>
> > >>
> > >> Hello Mansour,
> > >>
> > >> Not sure why you would need the extra Emplyoee.depId.
> > >>
> > >> I would do what you would like this way:
> > >>
> > >> Employee emp = new Employee() ;
> > >> emp.setId("SOME_ID_1") ;
> > >> emp.setDepartment(em.getReference(Employee.class, "FINANCE");
> > >> em.persist(emp);
> > >>
> > >>
> > >> From the JavaDoc:
> > >> <T> T getReference(java.lang.Class<T> entityClass, java.lang.Object
> > >> primaryKey)
> > >> Get an instance, whose state may be lazily fetched. If the requested
> > >> instance does not exist in the database, the EntityNotFoundException
> > >> is thrown when the instance state is first accessed. (The persistence
> > >> provider runtime is permitted to throw the EntityNotFoundException
> > >> when getReference is called.) The application should not expect that
> > >> the instance state will be available upon detachment, unless it was
> > >> accessed by the application while the entity manager was open.
> > >>
> > >> Hope this helps.
> > >>
> > >>
> > >> John
> > >>
> > >
> > >
>
> The getReference method should work, regardless of the persistence unit -
> essentially you are setting an Object reference of Type Department with the
> primary key "FINANCE"- but you are not necessarily accessing the DB to
> obtain the Object. In essence, it is exactly what you want to do.
>
> John
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message