-->
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: Schemaexport with hashmap mapping
PostPosted: Mon May 10, 2004 1:11 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:32 am
Posts: 32
Hi:
I am tryingto use SchemaExport and trying to map a Hashmap. I have 2 classes : TestParent and TestChildren. TestParent contains a hashmap of children with date_id as the key(one to many mapping).



My mapping files and java classes:


public class TestParent implements Serializable {

/** identifier field */
private int parentId;
private Map children;


public TestParent() {
}

public int getParentId() {
return this.parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}

public Map getChildren() {
return this.children;
}

public void setChildren(Map children) {
this.children = children;
}


}



public class TestChild implements Serializable {

private int parentId;
private int childId;
private Date dateId;

public TestChild() {
}




public int getParentId() {
return this.parentId;
}

public void setParentId(int parentId) {
this.parentId = parentId;
}




public int getChildId() {
return this.childId;
}

public void setChildId(int childId) {
this.childId = childId;
}


public Date getDateId() {
return this.dateId;
}

public void setDateId(Date dateId) {
this.dateId = dateId;
}


}




<?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="TestParent"
table="TEST_TL_PARENT"
>

<id
name="parent_id"
type="int"
column="PARENT_ID"
>
<generator class="assigned" />
</id>



<map name="Children" table="Children">
<key column="parent_id"/>
<index column="date_id" type="java.util.Date"/>

<one-to-many class="TestChild" />
</map>


</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="TestChild"
table="TEST_TL_CHILD"
>

<id
name="childId"
type="int"
column="CHILD_ID"
>
<generator class="assigned" />
</id>

<property
name="dateID"
type="java.util.Date"
column="DATE_ID"
not-null="true"
/>

<property
name="parentID"
type="int"
column="PARENT_ID"
not-null="true"
/>



</class>
</hibernate-mapping>




I get the following error at run time :



C:\hibernate-2.1\bin>SchemaExport.bat C:\test\TestParent.hbm.xml C:\test\TestChild.hbm.xml

May 10, 2004 12:57:29 PM alter table TEST_TL_CHILD drop constraint FK39AEA9827B66B0D0
drop table TEST_TL_CHILD cascade constraints
drop table TEST_TL_PARENT cascade constraints
create table TEST_TL_CHILD (CHILD_ID NUMBER(10,0) not null, DATE_ID DATE not null, PARENT_ID NUMBER(10,0)
not null, parent_id NUMBER(10,0), date_id DATE, primary key (CHILD_ID))
May 10, 2004 12:57:31 PM net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
SEVERE: Unsuccessful: create table TEST_TL_CHILD (CHILD_ID NUMBER(10,0) not null, DATE_ID DATE not null,
PARENT_ID NUMBER(10,0) not null, parent_id NUMBER(10,0), date_id DATE, primary key (CHILD_ID))
May 10, 2004 12:57:31 PM net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
SEVERE: ORA-00957: duplicate column name

create table TEST_TL_PARENT (PARENT_ID NUMBER(10,0) not null, primary key (PARENT_ID))
alter table TEST_TL_CHILD add constraint FK39AEA9827B66B0D0 foreign key (parent_id) references TEST_TL_PA
RENT
May 10, 2004 12:57:32 PM net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
SEVERE: Unsuccessful: alter table TEST_TL_CHILD add constraint FK39AEA9827B66B0D0 foreign key (parent_id)
references TEST_TL_PARENT
May 10, 2004 12:57:32 PM net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
SEVERE: ORA-00942: table or view does not exist

May 10, 2004 12:57:32 PM net.sf.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete
May 10, 2004 12:57:32 PM net.sf.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:oracle:oci8:@DEV



I am not sure whyis SchemaExport is trying to create duplicate columns for TEST_TL_CHILD table.

TIA

splash


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 8:50 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Hi,

I'm not sure, but I think the problem comes from your model. In the child class you should have a property which type is ParentClass and not int (the parentID attribute). Because you should think in the object pradigm and not in relationnal paradigm.
Let Hibernate do this work for you!

What you really want is to know who is the parent of a child, not the parentId of the child... do you understand?

So, you have to change sevral things :

In the child class :
private int parentId; --> private TestParent parent;
change the getters and setters...

In the child mapping files :
the property parentId become a <many-to-one> association.

Tell me if it's Ok!

Charles


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 2:35 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:32 am
Posts: 32
Yes, that did the trick. Thanx


splash


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.