?
Solved

EntityManager.merge()  in JPA

Posted on 2011-03-02
11
Medium Priority
?
650 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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 2000 total points
ID: 35799813
> @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)

need to add orphanRemoval=true

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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