-->
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: Mapping the same class twice?
PostPosted: Tue Jun 01, 2004 11:59 am 
Newbie

Joined: Sun May 30, 2004 7:38 am
Posts: 7
I am having some difficulty mapping a section of my model but I am not sure if this is a problem with my model, my mapping or just plain unacceptable to Hibernate.

Here's a simplified extract of my classes:
Code:
class Person {
  private Long ID;       // PK
  private PersonName personName;
  private Set otherPersonNames = new HashSet();
  ...
}

class PersonName {
  private Long ID;        // PK
  private String firstName;
  private String lastName;
  private Long personID;    // FK
  ...
}

The idea is that every Person has an associated PersonName and 0..* PersonName's in the Set otherPersonName.

I have two tables in a MySQL database with columns directly reflecting the classes above. Both tables use the autoincrement option for key generation.

My latest attempt for the mappings are:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <class name="Person" table="person">
    <id name="ID" unsaved-value="null">
      <generator class="increment"/>
    </id>
    <one-to-one name="personName" class="PersonName"/>
    <set name="otherPersonNames" inverse="true" lazy="true" cascade="save-update">
      <key column="personID"/>
      <one-to-many class="PersonName"/>
    </set>
  </class>

  <class name="PersonName" table="personname">
    <id name="ID" type="java.lang.Long" unsaved-value="null">
      <generator class="increment"/>
    </id>
    <property name="firstName" not-null="true"/>
    <property name="lastName" not-null="true"/>
    <many-to-one name="personID" class="Person" column="ID"/>
  </class>
</hibernate-mapping>


When I run the following code...
Code:
...
  // Create a Person
  Person p = new Person();

  // Create a PersonName and populate
  PersonName pn = new PersonName();
  pn.setFirstName("John");
  pn.setLastName("Doe");

  // Associate PersonName with Person
  p.setPersonName(pn);

  // Start Hibernate and attempt to save graph
  Configuration cfg = new Configuration()
    .addClass(Person.class)
    .addClass(PersonName.class);
  SessionFactory sf = cfg.buildSessionFactory();

  Session sess = sf.openSession();
  Transaction t = sess.beginTransaction();
  sess.save(p);
  t.commit();
  sess.close();

...Hibernate starts up properly but gives the following error shortly after instantiating session factory (and much to my disappointment nothing is written to the database):
Code:
net.sf.hibernate.MappingException: Repeated column in mapping for class com.dynbio.core.person.PersonName should be mapped with insert="false" update="false": ID

I am rather new to Hibernate and have (re)read the Parent/Child mapping references but just can't figure out what I am doing wrong or how to express the mapping for what I want.

Any clues would be very welcome!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 1:24 pm 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
<many-to-one name="personID" class="Person" column="ID"/>
is being mapped to the same column as
<id name="ID" type="java.lang.Long" unsaved-value="null">
<generator class="increment"/>
</id>
since the column name defaults to the property or 'id' name.

I don't think you created a table with columns with the same name so check what column personID is meant to be mapped to.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 3:33 am 
Newbie

Joined: Sun May 30, 2004 7:38 am
Posts: 7
Thanks for that javamona, it helped me get a bit further.

However, I'm still unsure if what I want to do is acceptable as I still cannot get it to work. Without pouring lots of logs into the thread I will try to explain the basic requirement as simply as possible...

I want the Person class to have a 1..1 association with PersonName which is straightforward and represents a person's legal name.

BUT I also want Person to have an second 0..* association with PersonName to support potentially multiple aliases.

The database has two tables, Person and PersonName but this may not be the best approach.

A similar scenario exists when modeling relationships where a Relationship class refers to two Person classes at the same time. I have yet to try that out but I think both of these cases are quite tricky in terms of mapping.

Again, your comments are very welcome!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 7:25 am 
Beginner
Beginner

Joined: Mon Sep 29, 2003 10:32 pm
Posts: 35
Location: Toronto, Ontario
Your two tables should look like
Code:

table Person(
  id number primary key,
  name_id   number
);

table Name(
  id number primary key,
  first_name varchar2(20),
  last_name varchar2(20),
  person_id number null,
);



and your classes
Code:
class Person{
  private PersonName name;
  private Set aliases = new HashSet();

//other setters and getters

  public void addAlias(PersonName name){
     this.aliases.add(name);
    name.setPerson(this);
  }

}

class PersonName{
    private String firstName;
    private String lastName;
    private Person person;  //note not PersonID:long but the Person class

  //setters and getters

}


In reality, the PersonName class shouldn't have to know about the Person class (a unidirectional relationship) but I find for sanity, bidirectional relationships work fine for me.vNote that PersonName don't keep track of the foreign key of Person, but the Person class.



What solves you immediate problem, is just naming the columns with different names.


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.