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
Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
hibernate insert example 13 34
Java exception bubble up 2 21
Setup GlassFish 4 21
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

808 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