Solved

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

Posted on 2013-06-05
2
372 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
Comment Utility
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
Comment Utility
yes simple query is working.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now