Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

EJB3 Help needed: @PrimaryKeyJoinColumn not working

Thread view: 
Dave - 08 Nov 2007 19:10 GMT
I have to tables (FIELD and FIELDACCESS) in our DB that share the same
primary key. There is a one-to-one relationship from FIELD to
FIELDACCESS.  I've read two accounts of how to setup the annotations
to get this relationship to work but I haven't been able to get either
of them to work.  In both cases I have the same problem but I will
only present one case here.

The following is how "EJB3 In Action" says to do it:

@Entity
@Table(name = "FIELDNAME")
public class Field implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @Column(name = "fieldid")
   protected long oid;

   @OneToOne
   @PrimaryKeyJoinColumn
   protected FieldAccess fieldAccess;

}

@Entity
@Table(name = "FIELDACCESS")
public class FieldAccess implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @Column (name = "fieldid")
   private long oid;

   public long getOid() {
       return this.oid;
   }
}

The problem I'm having is with the "@OneToOne" annotation in Field.  I
get the error "Join column 'fieldAccess_fieldid' cannot be resolved".
However, it appears to me that "fieldid" does exist in FieldAccess.

Could someone tell me what I'm doing wrong?

Thanks,
Dave.
Frank Langelage - 08 Nov 2007 20:23 GMT
> I have to tables (FIELD and FIELDACCESS) in our DB that share the same
> primary key. There is a one-to-one relationship from FIELD to
[quoted text clipped - 37 lines]
> get the error "Join column 'fieldAccess_fieldid' cannot be resolved".
> However, it appears to me that "fieldid" does exist in FieldAccess.

Try this:
@PrimaryKeyJoinColumns( {
  @PrimaryKeyJoinColumn( name = "fieldid",
   referencedColumnName = "fieldid" )
} )

The default naming of the FK in a 1:1 relation is <target entity
name>_<PK column name of target> -> fieldAccess_fieldid.
If you want something else you'll have to define it like above.
Dave - 09 Nov 2007 18:58 GMT
Well, that kinda works.  It gets rid of the error but when I try to
persist
the objects, the FieldAccess entity is created before the Field
entity,
which causes an integrity constraint rule to fire because the Field
entity is the parent it has to be created first.  The FieldAccess
entity
cannot exist without the Field.

I swapping the annotations around but now I get the following error
when
I try to run my test:

org.hibernate.AnnotationException: Unknown mappedBy in:
ca.ums.core.entities.Field.fieldAccess, referenced property unknown:
ca.ums.core.entities.FieldAccess.field

Any ideas?

> > I have to tables (FIELD and FIELDACCESS) in our DB that share the same
> > primary key. There is a one-to-one relationship from FIELD to
[quoted text clipped - 48 lines]
> name>_<PK column name of target> -> fieldAccess_fieldid.
> If you want something else you'll have to define it like above.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.