Solved

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

Posted on 2013-06-05
2
375 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 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