-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to update foreign key on associated entity?
PostPosted: Tue Aug 05, 2008 1:36 pm 
Newbie

Joined: Fri Jul 25, 2008 5:22 am
Posts: 12
I have a one-to-one bidirectional relationship using the foreign key as:

member(id, ...)
memberaddress(id, ...)

The member has:

Code:
public class Member ...
{
...
    @Id
    @Column(name = "member_id")
    @GeneratedValue(strategy = AUTO)
    public int getId() {
        return id;
    }

    @OneToOne(optional = true, cascade = ALL, fetch = EAGER)
    @JoinColumn(name="member_id", unique = true, updatable = false)
    public MemberAddress getMemberAddress()
    {
        return memberAddress;
    }
...
}


The member address has:

Code:
public class MemberAddress ...
{
    @OneToOne(mappedBy = "memberAddress")
    public Member getMember()
    {
        return member;
    }

    @Id
    @Column(name = "member_id", updatable= false, unique = true)
    public int getId()
    {
        return id;
    }
...
}


In my application I call:

Code:
  // Member newMember = Member();
  ...
  MemberAddress ma = new
   stateOrProvince, zipOrpostalCode);
  ma.setId(newMember.getId());
  newMember.setMemberAddress(ma);
  ma.setMember(newMember);
  em.persist(newMember);


Both entities are updated to the MySQL DB. However, the MemberAddress id value (foreign/primary key) is not updated (it still has the -1 value that was set during construction).

How do I get Hibernate to update the weak entity MemberAddress's foreign key which references the parent entity? And actually I thought with all the annotations I was using, that this was suppose to be already taken care of for me. Or am I missing something?

Any help most appreciated.

_________________
“The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over.” - Hunter S. Thompson


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 05, 2008 2:35 pm 
Newbie

Joined: Fri Jul 25, 2008 5:22 am
Posts: 12
I've actually noticed that Hibernate isn't doing what the documentation says the annotations should do since after I run my application, I get this from the MySQL DB:

Code:
CREATE TABLE `memberaddress` (
  `member_id` int(11) NOT NULL,
  `mailingaddress` varchar(255) default NULL,
  `stateorprovince` varchar(255) default NULL,
  `ziporpostalcode` varchar(255) default NULL,
  PRIMARY KEY  (`member_id`)


FYI: I get the above DDL by running:
Code:
mysqldump -u mysqlaccount -p mydb  --no-data  >  script_file.sql

_________________
“The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over.” - Hunter S. Thompson


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 4:22 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I think the foreign key will be in the member table (member is the owning side of the realtionship). You should change you entities as follows:

Code:
public class Member ...
{
...
    @Id
    @Column(name = "member_id")
    @GeneratedValue(strategy = AUTO)
    public int getId() {
        return id;
    }

    @OneToOne(optional = true, cascade = ALL, fetch = EAGER)
    public MemberAddress getMemberAddress()
    {
        return memberAddress;
    }
...
}

public class MemberAddress ...
{
    @OneToOne(mappedBy = "memberAddress")
    public Member getMember()
    {
        return member;
    }

    @Id
    @GeneratedValue(strategy = AUTO)
    public int getId()
    {
        return id;
    }
...
}


You can add the additional @Column annotations if you desire.

The above setup generates the following DDL:

Code:
CREATE TABLE member
(
  member_id int4 NOT NULL,
  memberaddress_member_id int4,
  CONSTRAINT member_pkey PRIMARY KEY (member_id),
  CONSTRAINT fk892776ba9c783481 FOREIGN KEY (memberaddress_member_id)
      REFERENCES memberaddress (member_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE memberaddress
(
  member_id int4 NOT NULL,
  CONSTRAINT memberaddress_pkey PRIMARY KEY (member_id)
)


Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 12:28 pm 
Newbie

Joined: Fri Jul 25, 2008 5:22 am
Posts: 12
andydale wrote:
Hi,

I think the foreign key will be in the member table (member is the owning side of the realtionship). You should change you entities as follows:
...
Cheers,

Andy


Andy, thanks for your time. That does work although it has an extra id value in the member table (which thus breaks normalization because it contains nulls). My idea was that the weak entity should use the primary key of the parent entity. That is to say: the address table should have its primary key set as the foreign key reference to the member table. The problem is that the Hibernate documentation makes absolutely no mention whatsoever of how to force the weak entity to set its primary key as a foreign key to the parent table (at least not using annotations).

I did find a solution from a post made by Hibernate forum user efriedman (half way down the page) at:

http://forum.hibernate.org/viewtopic.php?p=2367400

It uses GenericGenerator with a foreign strategy. His solution generates the following in MySQL:

Code:
DROP TABLE IF EXISTS `address`;
CREATE TABLE `address` (
  `id` bigint(20) NOT NULL,
  `mem_city` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `FKBB979BF42EA0FBC4` (`id`)
)

DROP TABLE IF EXISTS `member`;
CREATE TABLE `member` (
  `id` bigint(20) NOT NULL auto_increment,
  `mem_name` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
)


Thus, if a member is generated, its primary key value is trickled down to the weak entity. But thanks, your solution also works - it's just another perspective.

:)

_________________
“The Edge... there is no honest way to explain it because the only people who really know where it is are the ones who have gone over.” - Hunter S. Thompson


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.