Solved

EntityManager.merge()  in JPA

Posted on 2011-03-02
11
603 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now