openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Boblitz John <john.bobl...@bertschi.com>
Subject RE: How to map an ID of a foreign key as a field
Date Tue, 24 Jun 2014 06:23:42 GMT
> -----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
View raw message