-->
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: unidirectional one-to-many set collection
PostPosted: Mon Sep 18, 2006 4:58 am 
Newbie

Joined: Mon Sep 18, 2006 3:38 am
Posts: 2
Hi!
I have to table:
terms:
CREATE TABLE TERMS(
TERM_ID CHAR(13) NOT NULL,
LANG CHAR(2) NOT NULL,
DESCR VARCHAR NOT NULL,
PRIMARY KEY(TERM_ID,LANG)
)
codetable:
CREATE TABLE CODETABLE(
CODE CHAR(15) NOT NULL,
TABLE CHAR(8) NOT NULL,
TERM_ID CHAR(13) NOT NULL,
PRIMARY KEY(CODE,TABLE)
)

for relation for the two table i'd like use the field TERM_ID what is not primary in TERMS and not in TERMS.

I think for this relation is a one-to-many relation:
class codetable:

import java.util.Iterator;
import java.util.Set;

public class CodeTable {
private CodeTableId id;
private String term_id;
private Set terms;

public CodeTable() {
}
public Set getTerms() {
return terms;
}
public void setTerms(Set newTerms) {
terms = newTerms;
}
public CodeTableId getId() {
return id;
}
public void setId(CodeTableId id) {
this.id = id;
}
static public CodeTableId createId(String code, String table) {
return new CodeTableId(code, table);
}
public String getTerm_id() {
return term_id;
}
public void setTerm_id(String term_id) {
this.term_id = term_id;
}
}

CodeTableId:
import java.io.IOException;
import java.io.Serializable;

public class CodeTableId implements Serializable{
private String code;
private String table;

public CodeTableId(){
}
public CodeTableId(String code, String table) {
setCode(code);
setTable(table);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
private void writeObject(java.io.ObjectOutputStream s)throws IOException{
s.writeObject(code);
s.writeObject(table);
}
private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException{
code = (String)s.readObject();
table = (String)s.readObject();
}
}

mapping:
<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.ignis.db.lang">
<class name="CodeTable" table="codetable">
<composite-id name="id" class="CodeTableId" mapped="false" access="property">
<key-property name="Code" type="java.lang.String" column="code"/>
<key-property name="Table" type="java.lang.String" column="table"/>
</composite-id>
<property name="Term_id" not-null="false"/>
<set name="Terms" cascade="all">
<key column="term_id" unique="false" not-null="true" property-ref="Term_id"/>
<one-to-many class="Terms" not-found="ignore" />
</set>
</class>
</hibernate-mapping>

terms:
public class Terms {
private TermsId terms;
private String lang;
private String term_id;

private String descr;

public Terms(){

}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public TermsId getTerms() {
return terms;
}
public void setTerms(TermsId terms) {
this.terms = terms;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
static public TermsId createId(String terms_id,String lang){
return new TermsId(terms_id,lang);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

TermId:
public class TermsId {
private String lang;
private String term_id;
public TermsId(String term_id,String lang){
setTerm_id(term_id);
setLang(lang);
}
public TermsId(){
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getTerm_id() {
return term_id;
}
public void setTerm_id(String term_id) {
this.term_id = term_id;
}
}

Mapping:
<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.ignis.db.lang">
<class name="Terms" table="Terms">
<composite-id name="id" class="TermsId" mapped="false" access="property">
<key-property name="Term_id" type="java.lang.String" column="term_id"/>
<key-property name="Lang" type="java.lang.String" column="lang"/>
</composite-id>

<property name="Descr" not-null="false" type="java.lang.String"/>
<property name="Lang" not-null="false" type="java.lang.String"/>
</class>
</hibernate-mapping>

If i run the session.get(CodeTable.class,new CodeTableId(..,..));
i became a error: duplicated map for terms_id in TERMS and i must uset it with update="false" and insert="false".
where must i use it? i try it codetable mapping in <set>,<key>,<one-to-many> but not worked.

it isn't a parent/child relation, because i'd like use one term for many codetable. the TERMS_ID is not unique in TERMS and not in CODETABLE.

what do i wrong?
thanx:
ignis


Top
 Profile  
 
 Post subject: 2. try
PostPosted: Wed Sep 20, 2006 7:50 am 
Newbie

Joined: Mon Sep 18, 2006 3:38 am
Posts: 2
So. First i draw the database:
Code:
+----------+     +------------+
| Terms    |     | Codetable  |
+----------+     +------------+
| Term_id  |     | Table      |
| Lang     |     | Code       |
| Descr    |     | Term_id    |
+----------+     +------------+


because the first try not worked, and nobody can help, i try to add a Id column (as integer) to terms, and modify the mapping and class:
Code:
public class Terms {
   private int id;
   private String lang;
   private String term_id;
   
   private String descr;
   
   public Terms(){
      
   }
   public String getDescr() {
      return descr;
   }
   public void setDescr(String descr) {
      this.descr = descr;
   }
   public String getLang() {
      return lang;
   }
   public void setLang(String lang) {
      this.lang = lang;
   }
   public String getTerm_id() {
      return term_id;
   }
   public void setTerm_id(String term_id) {
      this.term_id = term_id;
   }

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

Mapping:
Code:
<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.ignis.db.lang">
   <class name="Terms" table="Terms">
      <id name="id" type="int" column="id"/>
      <property name="Term_id" not-null="false"/>
   </class>
</hibernate-mapping>

(the codetable still the same)
i run the test program and i became a error:
Code:
Exception in thread "main" java.lang.ClassCastException: java.lang.String
   at org.hibernate.type.ComponentType.toLoggableString(ComponentType.java:329)
   at org.hibernate.pretty.MessageHelper.collectionInfoString(MessageHelper.java:284)
   at org.hibernate.engine.CollectionLoadContext.getLoadingCollection(CollectionLoadContext.java:141)
   at org.hibernate.type.CollectionType.getCollection(CollectionType.java:488)
   at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:341)
   at org.hibernate.type.CollectionType.resolve(CollectionType.java:335)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
   at org.hibernate.loader.Loader.doQuery(Loader.java:717)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
   at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:799)
   at org.hibernate.impl.SessionImpl.get(SessionImpl.java:792)
   at org.ignis.book.BookTest.main(BookTest.java:40)

what's wrong?

thanx:
Ignis


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.