-->
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.  [ 8 posts ] 
Author Message
 Post subject: how to assurance three table relationship.....
PostPosted: Sun Mar 13, 2005 10:51 am 
Newbie

Joined: Sun Mar 13, 2005 10:40 am
Posts: 8
i have three table :
user: userId
userName
------------------
department : deptId


Top
 Profile  
 
 Post subject: Re: how to assurance three table relationship.....
PostPosted: Sun Mar 13, 2005 11:04 am 
Newbie

Joined: Sun Mar 13, 2005 10:40 am
Posts: 8
I have three table like there:
--------------------------------------------------------------------------------
UserTable : userId userName
PositionTable : positionId positionName
DepartmentTable:deparmentId; departmentName
User_Pst_DeptTable: userId positionId deparmentId orderId
-----------------------------------------------------------------------------
Note : one user have many positon and can in many department

how to assurance the relationship use hibernate(or somebady can tell me how to write the hbm.xml files.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 1:30 pm 
Newbie

Joined: Sat Mar 12, 2005 12:01 pm
Posts: 8
The mapping below should give you what you are asking for, but, I'm not sure its what you want. The model doesn't seem to create a strong relation ship between Department and Position, which would seem necessary.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="data.User"
table="User"
>

<id
name="userId"
column="userId"
type="java.lang.Long"
>
<generator class="native"/>

</id>

<set
name="departments"
table="User_Pst_Dept"
lazy="false"
cascade="all"
sort="unsorted"
>

<key
column="userId"
>
</key>

<many-to-many
class="data.Department"
column="departmentId"
outer-join="true"
/>

</set>

<set
name="position"
table="User_Pst_Dept"
lazy="false"
cascade="all"
sort="unsorted"
>

<key
column="userId"
>
</key>

<many-to-many
class="data.Position"
column="positionId"
outer-join="true"
/>

</set>

<property
name="userName"
type="java.lang.String"
column="userName"
/>
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 9:37 pm 
Newbie

Joined: Sun Mar 13, 2005 10:40 am
Posts: 8
dcracauer: thank you for your answer,and a question in your
mapping files: in my user_position_partment table have a colum orderId.
this marked which position is the first position for the user. how can I add this in the mapping files


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 13, 2005 11:24 pm 
Newbie

Joined: Sat Mar 12, 2005 12:01 pm
Posts: 8
I really recommend picking up a copy of Hibernate in Action. It is extraordinarily well written and thorough. I pulled all of this nearly directly out of the book.

I haven't actually tested this, but it should work:

<list
name="departments"
table="User_Pst_Dept"
lazy="true"
cascade="all"

>

<key
column="userId"/>
<index column="orderId"/>
<many-to-many
class="data.Department"
column="departmentId"
outer-join="true"
/>

</list>

<list
name="position"
table="User_Pst_Dept"
lazy="true"
cascade="all">

<key column="userId" />
<index column="orderId"/>
<many-to-many
class="data.Position"
column="positionId"
outer-join="true"
/>

</list>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 1:17 am 
Newbie

Joined: Sun Mar 13, 2005 10:40 am
Posts: 8
thanks for your help . I will try it ......
and my msn:to_jx@hotmail.com
if you use msn . please contact me ..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 15, 2005 5:43 am 
Newbie

Joined: Sun Mar 13, 2005 10:40 am
Posts: 8
dcracauer:
i have test ,but have some problem when insert the data .
can i write the mapping files like this:


<list
name="pstDepts"
table="tbl_emp_pst"
lazy="true"
inverse="false"
cascade="none">
<key
column="EMPLOYEE_ID" />
<index
column="ORDER_ID"/>
<many-to-many
class="ioffice.hrm.model.Position"
column="POSITION_ID"
outer-join="true" />
<many-to-many
class="ioffice.hrm.model.Department"
column="DEPARTMENT_ID"
outer-join="true" />
</list>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 15, 2005 5:48 am 
Newbie

Joined: Sun Mar 13, 2005 10:40 am
Posts: 8
dcracauer:

i have do the test . there are have some problem when insert the data.
and can i write the mapping file like this:

<list
name="pstDepts"
table="tbl_emp_pst"
lazy="true"
inverse="false"
cascade="none">
<key
column="EMPLOYEE_ID" />
<index
column="ORDER_ID"/>
<many-to-many
class="ioffice.hrm.model.Position"
column="POSITION_ID"
outer-join="true" />
<many-to-many
class="ioffice.hrm.model.Department"
column="DEPARTMENT_ID"
outer-join="true" />
</list>



when i run.i get the exception of :

java.lang.IllegalArgumentException: object is not an instance of declaring
class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:96)
at net.sf.hibernate.persister.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:307)
at net.sf.hibernate.proxy.HibernateProxyHelper.getIdentifier(HibernateProxyHelper.java:48)
at net.sf.hibernate.type.EntityType.toString(EntityType.java:84)
at net.sf.hibernate.type.PersistentCollectionType.toString(PersistentCollectionType.java:82)
at net.sf.hibernate.impl.Printer.toString(Printer.java:49)
at net.sf.hibernate.impl.Printer.toString(Printer.java:82)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2281)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2239)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.springframework.orm.hibernate.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:472)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:377)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:243)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy8.saveEmp(Unknown Source)
at ioffice.hrm.web.TestAction.execute(TestAction.java:152)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:105)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:152)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:90)
at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:99)
at org.springframework.orm.hibernate.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:171)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:163)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:208)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:259)
at com.caucho.server.port.TcpConnection.run(TcpConnection.java:341)
at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:467)
at com.caucho.util.ThreadPool.run(ThreadPool.java:408)
at java.lang.Thread.run(Thread.java:534)


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