Solved

EntityManager.merge()  in JPA

Posted on 2011-03-02
11
629 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

749 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