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.  [ 2 posts ] 
Author Message
 Post subject: Simple ?set? mapping for one column type string table
PostPosted: Wed Jul 23, 2008 12:37 pm 
Newbie

Joined: Sat Jul 19, 2008 9:33 am
Posts: 3
Location: US
I've a simple class for Foo:
public class Foo {
private Integer fooId;
private String division;
private Context context;
}
public class Context{
private Integer contextId;
private String description;
}

Now the kicker is that division in Foo is in a table called divisions that have one column in it called division of type string. I don’t want to create a class called division just to be able to map it like I did context but I do want Hibernate to “enforce” that the division is contained in the divisions table. Is there anyway to do this?

<hibernate-mapping>
<class name="Foo" table="Foo">
<id name="fooId" type="java.lang.Integer">
<column name="Foo_ID" />
<generator class="assigned" /></generator>
</id>
<property name="description" type="com.famc.model.hibernate.StringTrim">
<column name="Description" length="50" not-null="true" />
</property>
<many-to-one name="context" column="context_id" lazy="false"/>
</class>
<class name=" WorkflowContext" table="CONTEXT">
<id name="contextId" type="java.lang.Integer">
<column name="CONTEXT_ID" />
<generator class="assigned" />
</id>
<property name="description" type="java.lang.String">
<column name="Description" length="50" not-null="true" />
</property>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:42 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Couldn't you just do a secondary talbe mapping?

Essentially, that allows you to map one class to properties in two tables.

Mapping one Class to Two Tables

Image

Code:
package com.examscam.mappings;
import javax.persistence.*;import org.hibernate.Session;
import com.examscam.HibernateUtil;

@Entity
@Table(name="bar")
@SecondaryTable(name="foo")
public class FooBar {
  int id;
  String fooName;
  String barCode;

  @Id
  @GeneratedValue
  public int getId() {return id;}
  public void setId(int id) {this.id = id;}

  @Column(table="foo")
  public String getFooName() {return fooName;}
  public void setFooName(String fooName) {
    this.fooName = fooName;
  }

  /* no need for mapping-goes to default bar table */
  public String getBarCode() {return barCode;}
  public void setBarCode(String barCode) {
    this.barCode = barCode;
  }

  public static void main(String args[]) {
/*HibernateUtil needs FooBar.class in AnnotationConfiguration*/
    HibernateUtil.recreateDatabase();
    FooBar fb = new FooBar();
    fb.setBarCode("90210");
    fb.setFooName("ManChu");
     Session session = HibernateUtil.beginTransaction();
    session.save(fb);
    HibernateUtil.commitTransaction();
  }
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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