-->
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: Hibernate mapping for String array
PostPosted: Fri Jun 01, 2007 12:48 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
I am looking for solution to map an String array using hibernate. I am using XDoclet1.3 that comes with appfuse 1.9.4. It uses all XDoclet2 hibernate tags

Here is my array declartion in POJO:

private String[] interactionType = new String[0];

//here is the xdcoclet hibernate tags
/**
* @hibernate.array table="AI_INTERACTION_TYPE"
* @hibernate.one-to-many class="java.lang.String"
* @hibernate.key column="AI_ID"
* @hibernate.index column="IT_INDEX"
* @return
*/
public String[] getInteractionType() {
return interactionType;
}


And the generated *.hmb.xml file contains the following

<array name="interactionType" table="AI_INTERACTION_TYPE">
<key column="AI_ID"></key>
<index column="IT_INDEX"></index>
<one-to-many class="java.lang.String"/>
</array>


At runtime the following exception is thrown:
java.lang.ClassCastException: org.hibernate.type.StringType.

Can you some one post a good example to map String array using hibernate. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 10:03 am 
Newbie

Joined: Thu May 17, 2007 5:29 am
Posts: 13
I don't know much about annotations, but the mapping xml should be

<array name="interactionType" table="AI_INTERACTION_TYPE">
<key column="AI_ID"></key>
<index column="IT_INDEX"></index>
<element column="ACTUAL_COLUMN_IN_DB" type="string" />
</array>

(u need to use <element> and not <*-to-*> as this is a primitive type)

_________________
--
Saugata
Please rate if this post helps


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 10:40 am 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Thanks. it worked. here is the xdoclet hibernate tag:

/**
* @hibernate.array table="AI_INTERACTION_TYPE"
* @hibernate.element column="InteractionType" type="string"
* @hibernate.key column="AI_ID"
* @hibernate.index column="IT_INDEX"
* @return
*/
public String[] getInteractionType() {
return interactionType;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 12:48 pm 
Newbie

Joined: Fri Jun 08, 2007 12:37 pm
Posts: 2
Hi sjhyam, I hope you can help me. I am using hibernate with DB Progress and I have a problem with a field extend, that is like an array of integers. I saw you solution but it doesn't work. This is part of code:
/**
* @hibernate.array table="pub.Cliente"
* @hibernate.element column="diasfijoscobro" type="integer"
* @hibernate.key column="cvecliente"
* @hibernate.key column="cveempresa"
* @hibernate.index column="cvecliente"
* @return
*/
public Integer[] getDiasFijosCobro() {
return diasFijosCobro;
}


Table uses a composite id with fields cveempresa and cvecliente.

Thanks
Gore


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 2:03 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Hi Gore, I have created a sample class with your code and it created two tables, one for the base class and another for array. In your code "cvecliente" is used as column for both key and index. Change the index value as "cvecliente_index".

Also can you post the error message. Did your code create tables when xdoclet parsing is executed? Do you have problems creating tables or inserting data into tables?

/**
* @hibernate.array table="pub.Cliente"
* @hibernate.element column="diasfijoscobro" type="integer"
* @hibernate.key column="cvecliente"
* @hibernate.key column="cveempresa"
* @hibernate.index column="cvecliente"
* @return
*/
public Integer[] getDiasFijosCobro() {
return diasFijosCobro;
}




Thanks
Shyam


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 3:03 pm 
Newbie

Joined: Fri Jun 08, 2007 12:37 pm
Posts: 2
Hi again Shyam and thanks for reply. Well, I tried change index name and doesn't work. I don't generate DB because it is created and working in an application client-server, so I have had problems running unit test for DAO.

This is table definition:

Table Dump Table Field Index Table
Name Name Flags Count Count Label
----------------------------- -------- ----- ----- ----- -------------------
cliente cliente 68 25 ?

Description: una agenda de clientes
Storage Area: Schema Area


============================= FIELD SUMMARY =============================
============================= Table: cliente ============================

Flags: <c>ase sensitive, <i>ndex component, <m>andatory, <v>iew component

Order Field Name Data Type Flags Format Initial
----- ------------------------- ----------- ----- --------------- ----------
5 cvecliente inte im ->>>>>9 0
10 nombre char im x(50)
20 Telefono1 char xxx-xxx-xxxx
30 Telefono2 char xxx-xxx-xxxx
35 Fax char xxx-xxx-xxxx
50 Notas char[10] x(65)
60 rfc char i xxxx-xxxxxx-xxx
70 limitecredito deci-2 ->>,>>>,>>9.99 0
80 condicion inte >>>>>9 0
90 estatus logi m Activo/Desactiv yes
100 cveempresa inte i >>>>>9 0
110 cli_rel inte i >>>>>>9 0
120 interno logi Si/No no
130 saldo deci-3 i ->>,>>>,>>9.99 0
140 cveagente inte i >>>>9 0
150 nomabr char i x(11)
160 cvecuenta inte i >>>>>>9 0
170 cvetipocliente inte i >>>>>>9 0
190 preciofdirecto inte > 0
260 fecha date 99/99/99 TODAY
270 calleynumero char x(52)
280 colonia char x(28)
290 ciudad char x(28)
310 delegacion char x(28)
320 estado char x(20)
330 cp char x(8)
340 cvetipocambio inte >>>>>9 1
350 web char x(30)
360 email char x(30)
370 factoraje logi i Si/No no
380 poraplicar deci-2 ->,>>>,>>9.99 0
390 cveusuario inte m >>>9 0
400 cveforma inte >>>9 0
410 pais char x(17) MEXICO
420 saldopormoneda deci-2[25] ->>,>>>,>>9.99 0
430 cvecuentapormoneda inte[25] >>>>>>9 0
440 saldopapormoneda deci-2[25] ->>,>>>,>>9.99 0
450 extranjero logi i Si/No no
480 filial logi i Si/No no
490 prospecto logi i Si/No no
500 diasdevolucion inte >>>>>>9 0
510 local logi Si/No true
520 diasautodesactivar inte >>>>>9 0
530 numeroext char x(8)
540 numeroint char x(8)
550 foliocedula char x(10)
560 diasrevision inte >>>>>>>>>9 0
570 diaspago inte >>>>>>>>>9 0
600 requierepedido logi Si/No no
610 tipoimpuesto inte m >>>>>9 1
630 cvezonacobro inte >>>9 0
640 cvecobrador inte >>>9 0
650 horarevision char xx:xx - xx:xx
660 horapago char xx:xx - xx:xx
670 cvemediocliente inte >>>>9 0
680 freal date m 99/99/9999 TODAY
690 consignacion logi i Si/No no
700 cvealmacen inte >>>>>9 0
710 cveconsecutivo inte >>>>>9 0
720 nomfis char x(40)
730 apaterno char x(40)
740 amaterno char x(40)
750 facturarremisionar inte 9 0
760 consideraciones char x(15)
770 diasfijosrevision inte[5] >9 0
780 diasfijoscobro inte[5] >9 0
790 curp char x(18)
800 cveedi char x(30)

============================= INDEX SUMMARY =============================
============================= Table: cliente ============================

Flags: <p>rimary, <u>nique, <w>ord, <a>bbreviated, <i>nactive, + asc, - desc

Flags Index Name Cnt Field Name
----- -------------------------------- --- ---------------------------------
agecveclienterel 5 + cveempresa
+ prospecto
+ cveagente
+ cli_rel
+ cvecliente

agecveclienterelpro 5 + cveempresa
+ prospecto
+ cveagente
+ cli_rel
- cvecliente

agenombrerel 5 + cveempresa
+ prospecto
+ cveagente
+ cli_rel
+ nombre

agetiponombrerel 6 + cveempresa
+ prospecto
+ cveagente
+ cvetipocliente
+ cli_rel
+ nombre

clienterel 3 + cveempresa
+ cli_rel
+ cvecliente

cli_rel 2 + cveempresa
+ cli_rel

consignacion 4 + cveempresa
+ consignacion
+ cli_rel
+ nombre

w contenidonombre 1 + nombre

pu cvecliente 2 + cveempresa
+ cvecliente

cveclienterelpro 4 + cveempresa
+ prospecto
+ cli_rel
+ cvecliente

u cvecuenta 3 + cveempresa
+ cvecliente
+ cvecuenta

cvecuenta2 2 + cveempresa
+ cvecuenta

cveprospecto 4 + cveempresa
+ prospecto
+ cli_rel
- cvecliente

extranjero 2 + cveempresa
+ extranjero

factoraje 4 + cveempresa
+ factoraje
+ cli_rel
+ nombre

filial 2 + cveempresa
+ filial

nomabr 2 + cveempresa
+ nomabr

nombre 2 + cveempresa
+ nombre

nombrerel 4 + cveempresa
+ prospecto
+ cli_rel
+ nombre

u nombreunico 3 + cveempresa
+ cli_rel
+ nombre

a rfc 3 + cveempresa
+ cli_rel
+ rfc

saldo 3 + cveempresa
+ cli_rel
+ saldo

saldos 2 + cveempresa
+ saldo

tipocliente 2 + cveempresa
+ cvetipocliente

tiponombrerel 5 + cveempresa
+ prospecto
+ cvetipocliente
+ cli_rel
+ nombre


As you can see, there are five fields extend, three integer and two decimal and there are a unique primary composite key with cvecliente and cveempresa and several index more.

Right now, I have this in POJO:
/**
* @hibernate.array table="pub.Cliente"
* @hibernate.element column="diasfijoscobro" type="integer"
* @hibernate.key
* @hibernate.column="cvecliente"
* @hibernate.column="cveempresa"
* @hibernate.index column="cvecliente_index"
* @return
*/
and when I run unit test I get:
[junit] Testcase: testGetClientes(com.soficlassic.dao.ClienteDaoTest): Caused an ERROR
[junit] Error creating bean with name 'sessionFactory' defined in file [D:\Preproduccion\ExtranetSOFI\build\dao\gen\META-INF\applicationContext-hi
bernate.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FKCCCB93E9A969CFE8:pub.Cliente [id]))
must have same number of columns as the referenced primary key (pub.Cliente [id,cvecliente_index])
[junit] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [D:\Preproduccion\
ExtranetSOFI\build\dao\gen\META-INF\applicationContext-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingExce
ption: Foreign key (FKCCCB93E9A969CFE8:pub.Cliente [id])) must have same number of columns as the referenced primary key (pub.Cliente [id,cvecliente_i
ndex])
[junit] Caused by: org.hibernate.MappingException: Foreign key (FKCCCB93E9A969CFE8:pub.Cliente [id])) must have same number of columns as the refe
renced primary key (pub.Cliente [id,cvecliente_index])
[junit] at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
[junit] at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
[junit] at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1187)
[junit] at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1094)
[junit] at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1210)
[junit] at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:807)
[junit] at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:740)
[junit] at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:131)
[junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.j
ava:1062)
[junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java
:1029)
[junit] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:420
)
[junit] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
[junit] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:141)
[junit] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
[junit] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:156)
[junit] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
[junit] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
[junit] at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)
[junit] at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:77)
[junit] at org.springframework.test.AbstractSingleSpringContextTests.loadContextLocations(AbstractSingleSpringContextTests.java:138)
[junit] at org.springframework.test.AbstractSingleSpringContextTests.loadContext(AbstractSingleSpringContextTests.java:125)
[junit] at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:104)
[junit] at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:75)
[junit] at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
[junit]
[junit]
[junit] Test com.soficlassic.dao.ClienteDaoTest FAILED

Upsss, it's too much data, but I hope you can help and sorry but my english.
Thanks,
Gore


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.