Solved

EntityManager.merge()  in JPA

Posted on 2011-03-02
11
641 Views
Last Modified: 2012-05-11
Hi,

I have to create an Administrator Role with few  users assigned to it. I have create , update and remove functionality for the same.

I have a model : AdministratorModel which has a List of another model: AdministratorUserModel in it. Please find the code attached for this.

When i pass a list of new users assigned to the role and update it using merge function in JPA, it works fine.If i need to remove already existing Users, then i need to pass a new list of users without the user who is removed to merge function and it should ideally remove the users from database, but it is not happening.

I request you to look into the code and advice me on why remove record is not working. Please advice.
AdministratorModel:	

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name = "adid", nullable = false)
	private List<AdministratorUserModel> adminUserList;


AdministratorDAOImpl:

	public AdministratorModel update(AdministratorModel adminModel)
			throws Exception {
		this.entityManager = getEntityManager();
		EntityTransaction tx = entityManager.getTransaction();
		try {
			tx.begin();
			adminModel = entityManager.merge(adminModel);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			entityManager.close();
		}
		return adminModel;
	}

Open in new window

0
Comment
Question by:vijayprabakar
[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
  • 6
  • 5
11 Comments
 

Author Comment

by:vijayprabakar
ID: 35018845
Please find attached the table design
CREATE TABLE  "ETW_ADMINISTRATOR_USERS" 
   (	"ADID" NUMBER(38,0) NOT NULL ENABLE, 
	"STATUS" VARCHAR2(20), 
	"AD_USERKEY" NUMBER(38,0) NOT NULL ENABLE, 
	"USER_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	 CONSTRAINT "PK_ETW_ADMIN_USERS" PRIMARY KEY ("AD_USERKEY") ENABLE, 
	 CONSTRAINT "FK_ETW_ADMIN_USERS_ADMIN" FOREIGN KEY ("ADID")
	  REFERENCES  "ETW_ADMINISTRATOR" ("ADID") ENABLE, 
	 CONSTRAINT "FK_ETW_ADMINI_USER_USERS" FOREIGN KEY ("USER_KEY")
	  REFERENCES  "ETW_USERS_DETAILS" ("USER_KEY") ENABLE
   )
/

CREATE TABLE  "ETW_ADMINISTRATOR_USERS" 
   (	"ADID" NUMBER(38,0) NOT NULL ENABLE, 
	"STATUS" VARCHAR2(20), 
	"AD_USERKEY" NUMBER(38,0) NOT NULL ENABLE, 
	"USER_KEY" NUMBER(38,0) NOT NULL ENABLE, 
	 CONSTRAINT "PK_ETW_ADMIN_USERS" PRIMARY KEY ("AD_USERKEY") ENABLE, 
	 CONSTRAINT "FK_ETW_ADMIN_USERS_ADMIN" FOREIGN KEY ("ADID")
	  REFERENCES  "ETW_ADMINISTRATOR" ("ADID") ENABLE, 
	 CONSTRAINT "FK_ETW_ADMINI_USER_USERS" FOREIGN KEY ("USER_KEY")
	  REFERENCES  "ETW_USERS_DETAILS" ("USER_KEY") ENABLE
   )
/

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 35022400
try instead removing the users from the list using the clear() method


0
 

Author Comment

by:vijayprabakar
ID: 35052896
It did not help. I guess, there is something related to the keys in the two tables.
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 92

Expert Comment

by:objects
ID: 35053052
are you actually removing the child entities?
0
 

Author Comment

by:vijayprabakar
ID: 35356404
I am updating the table.
0
 
LVL 92

Expert Comment

by:objects
ID: 35357263
What do you mean updating the table?
0
 

Author Comment

by:vijayprabakar
ID: 35380613
it is equivalent to update <table> set <column>=<value> where <condition>.
0
 

Author Comment

by:vijayprabakar
ID: 35380620
I am trying to achieve the same using JPA. The merge method is not working as expected, I guess, I am doing something wrong with the mapping. Please advice.
0
 
LVL 92

Expert Comment

by:objects
ID: 35380628
you didn't answer my earlier q
0
 

Author Comment

by:vijayprabakar
ID: 35799804
The merge is not updating the link tables with respect to the primary key rather it is inserting a new row. Please advice.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 35799813
> @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)

need to add orphanRemoval=true

0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

690 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