openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sreedhar.sirigiri" <sreedhar.sirig...@gmail.com>
Subject RE: EntityExistsException when updating
Date Wed, 04 Jul 2007 06:45:42 GMT

Hi,

As suggested we have modified the code and its working fine. Thanks a lot
for the inputs.

for(Role lObj : user.getRoles()){
            	sObj.add(session.find(RoleDTO.class,lObj.getId()));
            }

Regards,
Sreedhar
 

Pinaki Poddar wrote:
> 
> 1. This following code block is possibly causing the failure. New
> RoleDTO instances are created as replica of user.getRoles().
> public void update(User user) {
> //.....
>         this.roles.clear();
>         for (Role r : user.getRoles()) {
>             roles.add(new RoleDTO(r));
>         }
>     }
> 
> 2. The programming style User-UserDTO, Role-RoleDTO and how User
> instances brings data to update UserDTO etc. is against the grain of JPA
> programming model. JPA API for detached/disconnected programming model
> for persistent data obviated the need for DTO. One must have strong
> reasons to use DTO with JPA -- and it will be useful for you to
> explicate the need that prompted you to use DTO. Unifying the entity
> that is persistent and the entity that is mobile (not Remote) to another
> tier will greatly simplfy the code and leverages the power of JPA.
> 
> 3. With the problem in hand, one approach will be to rewrite the above
> lines of code such that elements of User.getRoles() and this.roles  are
> combined based on id rather than being overwriiten by a new set of
> RoleDTO. 
> 
> 
> 
> Pinaki Poddar
> 972.834.2865
> 
> -----Original Message-----
> From: Sreedhar.sirigiri [mailto:sreedhar.sirigiri@gmail.com] 
> Sent: Tuesday, July 03, 2007 10:25 AM
> To: dev@openjpa.apache.org
> Subject: Re: EntityExistsException when updating
> 
> 
> Hi Patrick,
> 
> Please find the code below. We have 3 DTO and 1 implementation file. 
> 
> //UserDAOImpl.java
> 
>    public void update(SsContext context, User user) throws DaoException,
> UserException {
>     	EntityManager session = null;
>         String errCode = null;
>         UserTransaction utx = null;
>         try {
>         	//Creating the EntityManager instance.
>             session =
> ServiceLocator.getOpenJPASession(ServiceLocator.OPENJPA_SESSION_FACTORY)
> ;
> 
>             //beginning of OpenJPA transaction.
>         	utx = UserTransaction.class.cast(new
>  
> InitialContext().lookup("java:comp/UserTransaction"));
>         	utx.begin();
>         	session.joinTransaction();
>             //session.getTransaction().begin();
>             UserDTO userDTO =
> (UserDTO)session.find(UserDTO.class,user.getId());
>             //session.merge(user);
> 
> 			if (userDTO == null) {
> 				errCode =
> UserConstants.USER_WITH_NAME_NOT_FOUND_ERROR;
> 	
> logger.error(ErrorMessage.getErrorMessage(UserException.fBundleName,
> errCode, user.getName()));
> 				throw new UserException(errCode,
> UserException.USER_NOT_FOUND, null, user.getName());
> 			}
> 			if (userDTO.isPermanent() &&
> userDTO.isMuted(user)) {
> 				errCode =
> UserConstants.MODIFY_USER_ROLE_ERROR;
> 	
> logger.error(ErrorMessage.getErrorMessage(UserException.fBundleName,
> errCode, user.getName()));
> 				throw new UserException(errCode,
> UserException.IMMUTABLE_USER, null, user.getName());
> 			}
> //
> //            //Updating the User object.
> //            userDTO.setName(user.getName());
> //            userDTO.setPassword(user.getPassword());
> //            userDTO.setPasswordDuration(user.getPasswordDuration());
> //            userDTO.setPasswordResetDate(user.getResetDate());
> //
> //            Set<RoleDTO> sObj = new HashSet<RoleDTO>();
> //            for(Role lObj : user.getRoles()){
> //            	sObj.add(new RoleDTO(lObj));
> //            }
> //            userDTO.setRoles(sObj);
> 
>             //Commiting the OpenJPA persistent transaction.
> 		userDTO.update(user);
>             session.merge(userDTO);
>             utx.commit();
>             
> 
> //UserDTO.java
> @Entity
> @Table(name = "CGSS_USER")
> public class UserDTO {
> 
> 	@Column(name = "USER_ID")
> 	@Id
> 	@GeneratedValue(strategy=GenerationType.IDENTITY)
>     private Long id;
> 
> 	@Column(name = "USER_NAME")
>     private String name;
> 
> 	@Column(name = "PASSWORD")
>     private String password;
> 
> 	@Column(name = "PERMANENT", length=1)
> 	private String permanent="N";
> 
> 	@Column(name = "ENABLED")
> 	private String enabled;
> 
> 	@Column(name = "NEED_RESET")
> 	private String needReset;
> 
> 	@Column(name = "PASSWORD_RESET_DATE")
>     private Date passwordResetDate;
> 
> 	@Column(name = "PASSWORD_DURATION")
>     private int passwordDuration;
> 
> 	
> @ManyToMany(targetEntity=com.vormetric.server.dao.user.RoleDTO.class,
> fetch=FetchType.LAZY, cascade=CascadeType.ALL)
>     @JoinTable(name="CGSS_USER_ROLE_MAPPING",
> joinColumns=@JoinColumn(name="USER_ID", referencedColumnName="USER_ID"),
> inverseJoinColumns=@JoinColumn(name="ROLE_ID",
> referencedColumnName="ROLE_ID"))
>     private Set<RoleDTO> roles = new HashSet<RoleDTO>();
> 
>     public UserDTO() {}
> 
>     public UserDTO(User user) {
>         this.name = user.getName();
>         this.password = user.getPassword();
>         this.enabled = ((user.isEnabled())?"Y":"N");
>         this.needReset = ((user.needResetUponLogin())?"Y":"N");
>         this.passwordResetDate = new Date((new
> GregorianCalendar()).getTimeInMillis());
>         this.passwordDuration = user.getPasswordDuration();
>         this.setPermanent(user.isPermanent()?"Y":"N");
>         
>         for (Role r : user.getRoles()) {
>             roles.add(new RoleDTO(r));
>         }
>     }
> 
>     public void update(User user) {
>         if (!this.password.equals(user.getPassword())) {
>             this.password = user.getPassword();
>             this.passwordResetDate = new Date((new
> GregorianCalendar()).getTimeInMillis());
>         }
>         this.enabled = ((user.isEnabled())?"Y":"N");
>         this.needReset = ((user.needResetUponLogin())?"Y":"N");
>         this.passwordDuration = user.getPasswordDuration();
>         this.roles.clear();
>         for (Role r : user.getRoles()) {
>             roles.add(new RoleDTO(r));
>         }
>     }
> 
> 
> //RoleDTO.java
> @Entity
> @Table(name = "CGSS_ROLE")
> public class RoleDTO {
> 	java.lang.annotation.Annotation n=null;
> 
> 	@Column(name = "ROLE_ID", insertable=false, updatable = false)
> 	@Id
> 	@GeneratedValue(strategy=GenerationType.IDENTITY)
>     private Long id;
> 
> 	@Column(name = "ROLE_NAME", insertable=false, updatable = false)
>     private String name;
> 	
> 	
> 	
> @OneToMany(targetEntity=com.vormetric.server.dao.user.PrivilegeDTO.class
> ,
> fetch=FetchType.LAZY,cascade=CascadeType.ALL)
> 	private Set<PrivilegeDTO> privileges = new
> HashSet<PrivilegeDTO>();
> 
>     public RoleDTO() {}
> 
>     public RoleDTO(Role role) {
>         this.id = role.getId();
>         this.name = role.toString();
>     }
> 
> 
> //PrivilegeDTO.java
> @Entity
> @Table(name = "ROLE_PRIVILEGE")
> public class PrivilegeDTO {
> 	
> 	@Column(name = "PRIVILEGE_ID", insertable=false, updatable =
> false)
> 	@Id
> 	@GeneratedValue(strategy=GenerationType.IDENTITY)
>     private Integer id;
> 	
> 	@Column(name = "COMPONENT", insertable=false, updatable = false)
>     private String component;
> 	
> 	@Column(name = "PRIVILEGE", insertable=false, updatable = false)
>     private String privilege;
> 	
>     public PrivilegeDTO() {}
> 
> 
> 
> Sreedhar
> 
> 
> 
> Patrick Linskey-2 wrote:
>> 
>> Hi,
>> 
>> Can you post the code for the transaction in question? That'll help us
> 
>> get an idea of what is causing the problem.
>> 
>> -Patrick
>> 
>> On 7/3/07, Sreedhar.sirigiri <sreedhar.sirigiri@gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> When I'm trying to update records, the following exception is viewed 
>>> in log http://www.nabble.com/file/p11409813/Desktop.zip Desktop.zip .
> 
>>> Please find the files attached. Kindly help
>>>
>>> Caused by:
>>> <0.9.7-incubating nonfatal store error>
>>> org.apache.openjpa.persistence.EntityExistsException: An object of 
>>> type "com.vormetric.server.dao.user.RoleDTO" with oid 
>>> "com.vormetric.server.dao.user.RoleDTO-5" already exists in this 
>>> context; another cannot be persisted.
>>> FailedObject: com.vormetric.server.dao.user.RoleDTO@2efe2efe
>>>         at
>>> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2397)
>>>         at
>>>
> org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(SingleFieldManag
> er.java:757)
>>>         at
>>>
> org.apache.openjpa.kernel.SingleFieldManager.preFlushPCs(SingleFieldMana
> ger.java:732)
>>>         at
>>>
> org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager
> .java:634)
>>>         at
>>>
> org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager
> .java:559)
>>>         at
>>>
> org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager
> .java:475)
>>>         at
>>>
> org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.jav
> a:2678)
>>>         at
>>>
> org.apache.openjpa.kernel.PDirtyState.beforeFlush(PDirtyState.java:37)
>>>         at
>>>
> org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.
> java:854)
>>>         at
>>> org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1903)
>>>         at
>>> org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1863)
>>>         at
>>>
> org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:17
> 81)
>>>         at
>>>
> org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:149
> 1)
>>>         at
>>> org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
>>>         at
> org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
>>>         ... 33 more
>>> --
>>> View this message in context:
>>> http://www.nabble.com/EntityExistsException-when-updating-tf4017548.h
>>> tml#a11409813 Sent from the OpenJPA Developers mailing list archive 
>>> at Nabble.com.
>>>
>>>
>> 
>> 
>> --
>> Patrick Linskey
>> 202 669 5907
>> 
>> 
> 
> --
> View this message in context:
> http://www.nabble.com/EntityExistsException-when-updating-tf4017548.html
> #a11414646
> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
> 
> 
> Notice:  This email message, together with any attachments, may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
> entities,  that may be confidential,  proprietary,  copyrighted  and/or
> legally privileged, and is intended solely for the use of the individual
> or entity named in this message. If you are not the intended recipient,
> and have received this message in error, please immediately return this by
> email and then delete it.
> 
> 

-- 
View this message in context: http://www.nabble.com/EntityExistsException-when-updating-tf4017548.html#a11425682
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Mime
View raw message