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 Tue, 03 Jul 2007 15:25:17 GMT

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(SingleFieldManager.java:757)
>>         at
>> org.apache.openjpa.kernel.SingleFieldManager.preFlushPCs(SingleFieldManager.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.java: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:1781)
>>         at
>> org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
>>         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.html#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.


Mime
View raw message