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.  [ 6 posts ] 
Author Message
 Post subject: composit key and primary key in hibernate
PostPosted: Mon Jan 17, 2005 9:14 am 
Newbie

Joined: Mon Jan 17, 2005 9:05 am
Posts: 17
Hi,

I have a table which is

create table A(
id NUMBER(19,0),
name VARCHAR2(255),
primary key (id)

)

and another table
create table B(
id NUMBER(19,0),
version NUMBER(10,0),
filedName VARCHAR2(255),
primary key(id, version)

)

As you see that part of table B's primary key include table A's primary, how to realize in hibernate? could anyone give me some idea?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 17, 2005 7:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Try using Middlegen to generate the mappings for you. It might not be approrpiate for your production development (but its fine in that role) though it can (also) serve as a good learning tool.


Top
 Profile  
 
 Post subject: more details please
PostPosted: Mon Jan 17, 2005 9:37 pm 
Newbie

Joined: Mon Jan 17, 2005 9:05 am
Posts: 17
Hi,

Could you give me more detail? I am a new bie in hibernate, could you give me example code such as how to primary key class and how to map the file with database? thanks!


Top
 Profile  
 
 Post subject: you try it
PostPosted: Tue Jan 18, 2005 10:31 pm 
Newbie

Joined: Tue Jan 18, 2005 4:29 am
Posts: 6
package com.hibernate;

import java.util.Set;

/**
* @hibernate.class table = "A"
*/

public class A
{
private Integer id;

private String name;

private Set b;


public A()
{
super();
}

/**
* @hibernate.id generator-class = "assigned" unsaved-value = "null"
* type = "string"
* column = "id"
*/
public Integer getId()
{
return iid;
}
public void setFid(Integer id)
{
this.id = id;
}
/**
* @hibernate.property
* @return String
*/
public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
/**
* @hibernate.set inverse = "false" lazy = "false" cascade = "none"
* @hibernate.collection-key column = "id"
* @hibernate.collection-one-to-many class = "com.hibernate.B"
*/
public Set getB()
{
return b;
}
public void setB(Set b)
{
this.b = b;
}
}

package com.hibernate;

import java.io.Serializable;

public class BKey implements Serializable
{
private Integer id;

private Integer version;

/**
* @hibernate.property
*/
public Integer getVersion()
{
return version;
}

public void setVersion(Integer version)
{
this.version = version;
}

/**
* @hibernate.many-to-one class = "com.hibernate.A"
*/
public Integer getId()
{
return id;
}

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

public boolean equals(Object o)
{
if (o == null) { return false; }
if (getClass().equals(o.getClass())
&& id.equals(((BKey) o).getId())
&& version.equals(((BKey) o).getVersion()))
{
return true;
}
else
{
return false;
}

}

public int hashCode()
{
int result;
result = id.hashCode();
result = 29 * result + version.hashCode();
return result;
}

}

package com.hibernate;

/**
* @hibernate.class table = "B"
*/

public class B
{
private BKey id;

private String filedName ;

private A a;

/**
* @hibernate.id generator-class = "assigned"
*/
public BKey getId()
{
return id;
}
public void setId(BKey id)
{
this.id = id;
}
/**
* @hibernate.property
* @return String
*/
public String getFiledName()
{
return filedName ;
}

public void setFiledName(String filedName )
{
this.filedName = filedName ;
}

/**
* @hibernate.many-to-one column = "id" class = "com.hibernate.A"
* insert = "false" update = "false" not-null = "true"
*/
public A getA()
{
return a;
}
public void setA(A a)
{
this.a = a;
}
}


<?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="com.hibernate.A"
table="A"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="Integer"
unsaved-value="null"
>
<generator class="assigned">
</generator>
</id>

<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
/>

<set
name="b"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
>

<key
column="id"
>
</key>

<one-to-many
class="com.hibernate.B"
/>
</set>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Father.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


<?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="com.hibernate.B"
table="B"
dynamic-update="false"
dynamic-insert="false"
>

<composite-id
name="id"
class="com.hibernate.BKey"
>
<key-property
name="version"
type="java.lang.Integer"
column="version"
/>

<key-many-to-one
name="id"
class="com.hibernate.A"
column="id"
/>

</composite-id>

<property
name="filedName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="filedName"
/>

<many-to-one
name="a"
class="com.hibernate.A"
cascade="none"
outer-join="auto"
update="false"
insert="false"
access="property"
column="id"
not-null="true"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Childs.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: thanks sincedar
PostPosted: Wed Jan 19, 2005 4:52 am 
Newbie

Joined: Mon Jan 17, 2005 9:05 am
Posts: 17
Hi, sincedar, thanks for your codes.

I am trying it !

Could you also provide me the codes before sessionOPen() and close session(). I mean the calling class

my calling code is like this:
A test = new A();
//i do not need to assign the primary key "id" here because it is a foreign key from another table, and it is working in this table
persistenceSession.save(test);

B test2 = new B();

//????problem is here, what should I set in test2? I should not set the primary key here, isn't it? because this composit-id get "id" from table A
test2.setTest1(test);


persistenceSession.save(test2);

Ok, maybe confused you. table A's primary key is "id", table B's primary is composite-id, "id" and "version".

A's primary key in fact is a foreign key get from table C. Now table B's composite id will get "id" from table A, what should I do in my program? should I initialize the class "Bkey"? thanks!


Top
 Profile  
 
 Post subject: this is a example
PostPosted: Thu Jan 20, 2005 10:43 pm 
Newbie

Joined: Tue Jan 18, 2005 4:29 am
Posts: 6
mysql4 sql:
create table father(fid varchar(10) not null primary key,fname varchar(20))
create table childs(fid varchar(10) not null,cid varchar(10) not null,cname varchar(20),primary key(fid,cid))

java code:
--Father.java
package tutorial.hibernate;

import java.util.Set;

/**
* @hibernate.class table = "father"
*/

public class Father
{
private String id;

private String fname;

private Set childs;

public Father()
{
super();
}

/**
* @hibernate.id generator-class = "assigned" unsaved-value = "any" column =
* "Fid" type = "string"
*/
public String getId()
{
return id;
}

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

/**
* @hibernate.property
* @return String
*/
public String getFname()
{
return fname;
}

public void setFname(String fname)
{
this.fname = fname;
}

/**
* @hibernate.set table = "Childs" lazy = "false" inverse = "false" cascade =
* "save-update" sort = "unsorted"
* @hibernate.collection-key column = "fid"
* @hibernate.collection-one-to-many class = "tutorial.hibernate.Childs"
*/
public Set getChilds()
{
return childs;
}

public void setChilds(Set childs)
{
this.childs = childs;
}
}
--Childs.java
package tutorial.hibernate;

/**
* @hibernate.class table = "childs"
*/

public class Childs
{
private ChildsKey ckey;

private String cname;

private Father father;

/**
* @hibernate.id generator-class = "assigned" unsaved-value = "any" type =
* "ChildsKey"
* @hibernate.collection-key column = "cid"
* @hibernate.collection-key column = "fid"
*/
public ChildsKey getCkey()
{
return ckey;
}

public void setCkey(ChildsKey ckey)
{
this.ckey = ckey;
}

/**
* @hibernate.property
* @return String
*/
public String getCname()
{
return cname;
}

public void setCname(String cname)
{
this.cname = cname;
}

/**
* hibernate.many-to-one column = "fid" class = "tutorial.hibernate.Father"
* insert = "false" update = "false" not-null = "true"
* cascade = "save-update" outer-join = "auto"
*/
public Father getFather()
{
return father;
}

public void setFather(Father father)
{
this.father = father;
}
}
--ChildsKey.java
package tutorial.hibernate;

import java.io.Serializable;

public class ChildsKey implements Serializable
{
private String fid;

private String cid;

/**
* @hibernate.property column = "cid"
*/
public String getCid()
{
return cid;
}

public void setCid(String cid)
{
this.cid = cid;
}

/**
* @hibernate.property
* hibernate.many-to-one column = "fid" class = "tutorial.hibernate.Father"
*/
public String getFid()
{
return fid;
}

public void setFid(String fid)
{
this.fid = fid;
}

/**
* 以下两个方法一定要有啊
*/
public boolean equals(Object o)
{
if (o == null) { return false; }
if (getClass().equals(o.getClass())
&& fid.equals(((ChildsKey) o).getFid())
&& cid.equals(((ChildsKey) o).getCid()))
{
return true;
}
else
{
return false;
}

}

public int hashCode()
{
int result;
result = fid.hashCode();
result = 29 * result + cid.hashCode();
return result;
}

}
--Father.hbm.xml
<?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="tutorial.hibernate.Father"
table="father"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="Fid"
type="string"
unsaved-value="any"
>
<generator class="assigned">
</generator>
</id>

<property
name="fname"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="fname"
/>

<set
name="childs"
table="Childs"
lazy="false"
inverse="false"
cascade="save-update"
sort="unsorted"
>

<key
column="fid"
>
</key>

<one-to-many
class="tutorial.hibernate.Childs"
/>
</set>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Father.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>
--Childs.hbm.xml
<?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="tutorial.hibernate.Childs"
table="childs"
dynamic-update="false"
dynamic-insert="false"
>

<composite-id
name="ckey"
class="tutorial.hibernate.ChildsKey"
unsaved-value="any"
>
<key-property
name="cid"
type="java.lang.String"
column="cid"
/>

<key-property
name="fid"
type="java.lang.String"
column="fid"
/>

</composite-id>

<property
name="cname"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="cname"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Childs.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

testcase code:
...
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

Father f = (Father) session.load(Father.class, "f1");
System.out.println(f.getFname());
Set set = f.getChilds();
Iterator it = set.iterator();
while (it.hasNext())
{
Childs c = (Childs) it.next();
System.out.println(" " + c.getCname());
}

ChildsKey ckey = new ChildsKey();
ckey.setCid("c2_1");
ckey.setFid("f2");
Childs c2 = new Childs();
c2.setCkey(ckey);
c2.setCname("c2_1name");

Set s = new HashSet();
s.add(c2);

Father f2 = new Father();
f2.setId("f2");
f2.setFname("f2name");

f2.setChilds(s);

session.save(f2);

tx.commit();
HibernateUtil.closeSession();
...


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