Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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

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
mmingfeilam
Asked:
mmingfeilam
1 Solution
 
ManishLeadCommented:
is Simple query working?
like select t from task t where ..
and do u get values in supersede after above query execute?
0
 
mmingfeilamAuthor Commented:
yes simple query is working.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now