Solved

how to represent many-to-many relationship in java hibernate?

Posted on 2013-06-05
2
380 Views
Last Modified: 2013-07-22
i have two tables, task and task_supersede.  task_supersede is a junction table containing  the id of tasks that got superseded and the id of the tasks that supersede.  

in the DAO layer, i have something like this:
	@ManyToMany (cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch=FetchType.LAZY)
	@JoinTable(name = "ESV_TASK_SUPERCEDE", joinColumns = @JoinColumn(name = "TASK_ID"), inverseJoinColumns = @JoinColumn(name = "SUPCED_TASK_ID"))	
	private List <Task> supersede;	

Open in new window


my question is how do you represent the junction table?  also how do i do a search?  i tried
String q = "Select task from com.ge.transportation.eservices2.dbaccess.model.wsa.Task task " +
				" LEFT OUTER JOIN task.taskDescription description  " + 

				" INNER JOIN task.supersede supersede  " +
				"  ";
...
whereClause += " and supersede.taskNumber in ('STR052111')";

Open in new window


or

String q = "Select task from com.ge.transportation.eservices2.dbaccess.model.wsa.Task task " +
				" LEFT OUTER JOIN task.taskDescription description  " + 

				" INNER JOIN task.supersede supersede  " +
				"  ";
...

if(null != superCedes && superCedes.size() > 0 ){
			List<String> superCedesQuery = new ArrayList<String>();

			for (String superCede : superCedes) {
				superCedesQuery.add(" supersede.taskNumber like '%" + superCede.toUpperCase() + "%'");
			}
			whereClause += " and ("+ StringUtils.join(superCedesQuery, " or ")  +")";

		}

Open in new window


but i either get nothing back or an exception:
2013-06-05 19:40:44,201[qtp174200572-120]|ERROR|com.ge.transportation.eservices2.service.wsatask.impl.TaskServiceImpl|Exception occored
java.lang.IllegalArgumentException: org.hibernate.QueryException: illegal attempt to dereference collection [task0_.TASK_ID.supersede] with element property reference [taskNumber] [Select task from com.ge.transportation.eservices2.dbaccess.model.wsa.Task task  LEFT OUTER JOIN task.taskDescription de
scription     where description.locale.localeId = 4 and ( task.supersede.taskNumber like '%STR052111%') ORDER BY task.insertedTimeStamp DESC ]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)

thanks.
0
Comment
Question by:mmingfeilam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Manish earned 500 total points
ID: 39234340
is Simple query working?
like select t from task t where ..
and do u get values in supersede after above query execute?
0
 

Author Comment

by:mmingfeilam
ID: 39266630
yes simple query is working.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question