Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Mapping single parent column to multiple child columns
PostPosted: Thu Mar 23, 2017 3:04 am 
Newbie

Joined: Thu Mar 23, 2017 2:25 am
Posts: 2
I have two tables one a users table and the other table is for jobs. The users table stores records of users of different types, for example there is admin user, worker, and client user. The jobs table stores records of jobs, each job is the responsibility of the three type of users(This is the business rule). So each job record needs to reference an adminID, clientID, and workerID, which are all just the userID. I tried mapping this in hibernate and JPA but it gave me errors saying a single column cannot be mapped to multiple columns. Please help out!



Code:
<!-- Mapping for user -->
   <one-to-many name="job" mapped-by="user" target-entity="domain.job" fetch="LAZY" access="FIELD" />
   
   <!-- mapping on the job -->
   <many-to-one name="admin">
      <join-column name="userId" />
   </many-to-one>
   
   <many-to-one name="client">
      <join-column name="userId" />
   </many-to-one>
   
   <many-to-one name="worker">
      <join-column name="userId" />
   </many-to-one/>
   
   <!-- POJOs -->
   class User{
   
        private int userId;
        private Job job;
      
       //rest of the code  . . .
   }
   
   class Job {
      private int jobId;
      private User admin;
      private User client;
      private User worker;
      
      //rest of the code . . .
   }


Top
 Profile  
 
 Post subject: Re: Mapping single parent column to multiple child columns
PostPosted: Thu Mar 23, 2017 5:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1385
Why do you use the legacy HBM mappings in 2017? JPA annotations are more expressive, and better supported in latest versions of Hibernate.

Since you have 3 many-to-one associations, I guess you need 3 different columns, right?

Code:
   
   <many-to-one name="admin">
      <join-column name="admin_id" />
   </many-to-one>
   
   <many-to-one name="client">
      <join-column name="client_id" />
   </many-to-one>
   
   <many-to-one name="worker">
      <join-column name="worker_id" />
   </many-to-one/>


In your mapping, you used only one column (userId), but that would just create 3 associations that are identical.

Or, in case you use a single userId FK column, you need an extra column as a discriminator for Admin, Client, Worker. Or maybe you need to use inheritance and the User class is a base class while Admin, Client, Worker are subclasses.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Mapping single parent column to multiple child columns
PostPosted: Thu Mar 23, 2017 6:16 am 
Newbie

Joined: Thu Mar 23, 2017 2:25 am
Posts: 2
Okay perhaps the problem is with the design of my tables. The reason why I did not add three columns in the user table, each for the adminId, clientId, and workerId they are all just userId's in the same table.

I did think of that though but the problem would be that if I needed to add an admin for example, the clientId and workderId colomns would be nulls. I Dont know if that would be bad or not since they are foreign keys.

Could you please provide an example that is similar to the situation I have?


Top
 Profile  
 
 Post subject: Re: Mapping single parent column to multiple child columns
PostPosted: Thu Mar 23, 2017 7:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1385
Even if you have 3 properties in your JPA entity, as long as you only have a single column in the DB table, all properties will have the same value when you read the record from the DB. Try writing a test case and see for yourself.

And yes, you need to change your mapping since it's not correct.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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.