-->
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.  [ 3 posts ] 
Author Message
 Post subject: @OneToOne and @PrimaryKeyJoinColumn mapping usage
PostPosted: Thu Oct 16, 2008 5:19 am 
Newbie

Joined: Fri Feb 27, 2004 6:49 pm
Posts: 18
Location: Paris, France
Hibernate version: 3.2.4.sp1
Hibernate annotation version: 3.3.0.ga
Hibernate entity manager version: 3.3.1.ga
Database: Oracle 10g

Hi,

I've been trying to do a one-to-one relationship using the same primary key. It seems that it's way more complex than expected. First, here's my final working result.

Deal is aggregating a DealState objet in a one-to-one relationship with a cascade

Code:
@Entity
@Table(name = "DEAL")
@SequenceGenerator(name = "DEAL_SEQ", sequenceName = "DEAL_SEQ")
public class Deal {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DEAL_SEQ")
  public long getId()
  {
    return id;
  }

  @OneToOne(optional = true, cascade = CascadeType.ALL)
  @PrimaryKeyJoinColumn
  @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @ForeignKey(name = "FK_DEAL_STATE_DEAL")
  public DealState getState()
  {
    return state;
  }

  public void setWorkflowState(DealState state)
  {
    this.state = state;
    this.state.setDeal(this);
  }
}

@Entity
@Table(name = "DEAL_STATE")
public class DealState {

  @Id
  @GeneratedValue(generator = "system-foreign")
  @GenericGenerator(name = "system-foreign", strategy = "foreign", parameters = {
    @Parameter(name = "property", value = "deal")
  })
  @Column(name = "ID_DEAL", nullable = false, insertable = true, updatable = false)
  public long getId()
  {
    return id;
  }

  @OneToOne(optional = false, mappedBy = "state")
  public Deal getDeal()
  {
    return deal;
  }

  public void setDeal(Deal deal)
  {
    this.deal = deal;
  }
}


Then the comments:
    1- I had to put a back pointer from DealState to Deal
    2- This back pointer is used with the foreign generator strategy to set the id to the Deal state. This strategy doesn't seem documented.
    3- On getState, @OneToOne was set optional even if it's not. The optional parameter is used to deduce the constrained parameter. If optional is not true, the entities aren't same in the right order. I don't understand why these not explicit constrained parameter to set (like in the hbm.xml mapping).

My obvious question is:

Am I doing it right? Isn't there any easier way to do the same thing? (which could explain why the documentation doesn't provide a complete explanation)

Thanks,
Henri


Top
 Profile  
 
 Post subject: Which annotations ?
PostPosted: Tue Dec 02, 2008 10:07 am 
Newbie

Joined: Tue Dec 02, 2008 10:03 am
Posts: 1
Location: London
I have not be able to run this example successfully : one possible reason is hibernate/java annotations. Which should I use ?

Thanks

Martin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2008 3:52 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Does the following unidirectional one-to-one 'DealState -> Deal' mapping suit your needs?

Deal.java

Code:
@Entity
@Table(name = "DEAL")
public class Deal {

    private Long id;

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

    public void setId(Long id) {
        this.id = id;
    }
}



DealState.java

Code:
package com.hibernate.entity;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;

@Entity
@Table(name = "DEAL_STATE")
public class DealState {

    private Long id;
    private Deal deal;

    @Id
    @GeneratedValue(generator = "foreign")
    @GenericGenerator(name = "foreign", strategy = "foreign", parameters = {
            @Parameter(name = "property", value = "deal")
    })
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    @PrimaryKeyJoinColumn
    public Deal getDeal() {
        return deal;
    }

    public void setDeal(Deal deal) {
        this.deal = deal;
    }
}



The following code snippet makes Deal and DealState objects persistent:

Code:
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = session.beginTransaction();

        Deal deal = new Deal();
        DealState state = new DealState();
        state.setDeal(deal);
        session.saveOrUpdate(state);

      tx.commit();
      session.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.