Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

EntityManager.merge()  in JPA

Posted on 2011-03-02
11
Medium Priority
?
659 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
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.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

609 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