-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: JPA - unable to update the entity
PostPosted: Mon Oct 27, 2014 9:25 am 
Newbie

Joined: Mon Oct 27, 2014 9:21 am
Posts: 3
We are having a JPA project with Hibernate as the provider running in Tomcat server. Problem is when I am trying to update the entity, it is not happening and leading to exceptions. HostConfiguration is the entity that I want to update using a class SystemDaoImpl. HostConfiguration here in turn has InterfaceConfiguration as the child entity. HostConfiguration has 2 InterfaceConfiguration(s) - trusted and untrusted. My aim is to (a) update one of the interface configurations i.e. remove the existing interface config and replace with a new one (b) remove the the full host config with all interface configs also and then add a new one. I want to know how can I do this ? I have tried various ways/combinations after going through multiple forums over the net. The code below gives me this exception -

org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [com.cisco.cpm.ipep.system.InterfaceConfiguration#InterfaceConfiguration.IfConfigPKey[type=TRUSTED,ipAddress=/7.7.20.18]]

Appreciate any help on this, thanks.

//SystemDaoImpl.java

public class SystemDaoImpl {

//code omiited
@Override

public void setHostConfig(HostConfiguration hostConf) {

HostConfiguration hc = em.find(HostConfiguration.class, hostConf.getHostname());
if(hc != null) {
InterfaceConfiguration trusted = hc.getTrustedInterface();
InterfaceConfiguration untrusted = hc.getUntrustedInterface();

if(!hostConf.equals(hc) || !trusted.equals(hostConf.getTrustedInterface()) || !untrusted.equals(hostConf.getUntrustedInterface())) {
log.info("removing the existing hostconf");
em.remove(hc);
InterfaceConfiguration.IfConfigPKey tpkey = new InterfaceConfiguration.IfConfigPKey();
tpkey.setType(Type.TRUSTED);
tpkey.setIpAddress(trusted.getIpAddress());
InterfaceConfiguration trusted1 = em.find(InterfaceConfiguration.class, tpkey);
if(trusted1 != null) {
em.remove(trusted1);
}
InterfaceConfiguration.IfConfigPKey upkey = new InterfaceConfiguration.IfConfigPKey();
tpkey.setType(Type.UNTRUSTED);
tpkey.setIpAddress(untrusted.getIpAddress());
InterfaceConfiguration untrusted1 = em.find(InterfaceConfiguration.class, upkey);
if(untrusted1 != null) {
em.remove(untrusted1);
}
log.info("now adding the new hostconf: " + hostConf);
em.merge(hostConf);
} else {
log.info("hostConf and existing hostConf are equal");
}
}

//some code removed ...
}

HostConfiguration.java

@Entity

@Table(name = "host_configurations")

@XmlRootElement(name = "host_configuration")

@XmlAccessorType(XmlAccessType.FIELD)

public class HostConfiguration implements Cloneable {
public enum HostType {

PRIMARY, SECONDARY, SERVICE
}



@Id

@XmlElement

private String hostname;

@XmlElement

private String peerHostname;

@Enumerated

@XmlElement

private HostType type = HostType.PRIMARY;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

@JoinColumns({

@JoinColumn(name = "trusted_interface_type", referencedColumnName = "type", nullable = false),
@JoinColumn(name = "trusted_interface_ip_address", referencedColumnName = "ip_address", nullable = false) })

@XmlElement(name = "trusted_interface")

private InterfaceConfiguration trustedInterface;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

@JoinColumns({

@JoinColumn(name = "untrusted_interface_type", referencedColumnName = "type", nullable = false),

@JoinColumn(name = "untrusted_interface_ip_address", referencedColumnName = "ip_address", nullable = false) })

@XmlElement(name = "untrusted_interface")

private InterfaceConfiguration untrustedInterface;

//code removed

}

InterfaceConfiguration.java

@Entity

@Table(name = "interface_configurations")

@IdClass(InterfaceConfiguration.IfConfigPKey.class)

@XmlAccessorType(XmlAccessType.FIELD)

public class InterfaceConfiguration implements Cloneable {
public enum Type {
TRUSTED, UNTRUSTED
}

@Id

@Enumerated

@XmlElement

private Type type;

@Id

@Column(name = "ip_address")

@XmlElement(name = "ip_address")

@XmlJavaTypeAdapter(value = InetAddressXmlAdapter.class)

private InetAddress ipAddress;

@XmlElement

private Subnet netmask;

@Column(name = "vlan_native")

@XmlElement(name = "vlan_native")

private boolean vlanNative;

@XmlElement

@XmlJavaTypeAdapter(value = InetAddressXmlAdapter.class)

private InetAddress gateway;

@Column(name = "vlan_id")

@XmlElement(name = "vlan_id")

private int vlanId;

//code removed

}


Top
 Profile  
 
 Post subject: Re: JPA - unable to update the entity
PostPosted: Mon Oct 27, 2014 9:35 am 
Newbie

Joined: Sat Oct 25, 2014 10:31 am
Posts: 7
As I understand, the exception says that you need to remove the object from accosiations before deleting it. Find all objects having link to object you are deleting, remove that links, and after that remove object.

Use the right mark for code, please.


Top
 Profile  
 
 Post subject: Re: JPA - unable to update the entity
PostPosted: Mon Oct 27, 2014 2:00 pm 
Newbie

Joined: Mon Oct 27, 2014 9:21 am
Posts: 3
I am not using InterfaceConfiguration entities/objects anywhere else (only HostConfiguration uses it).


Top
 Profile  
 
 Post subject: Re: JPA - unable to update the entity
PostPosted: Mon Oct 27, 2014 2:22 pm 
Newbie

Joined: Sat Oct 25, 2014 10:31 am
Posts: 7
It's difficult to read your code, but I found annotation @Cascade with attribute deleteOrphan. Maybe you're attempting to delete the entity that was deleted already?


Top
 Profile  
 
 Post subject: Re: JPA - unable to update the entity
PostPosted: Tue Oct 28, 2014 2:31 am 
Newbie

Joined: Mon Oct 27, 2014 9:21 am
Posts: 3
OK, here is my code formatted. There is one-to-one relation between the HostConfiguration and InterfaceConfiguration. Please let me know what is the correct way to dissociate one InterfaceConfiguration and add a new InterfaceConfiguratuon to the HostConfiguration ?

Appreciate your response!

Code:
public class SystemDaoImpl {

   // code omiited
   @Override
   public void setHostConfig(HostConfiguration hostConf) {
      HostConfiguration hc = em.find(HostConfiguration.class,
            hostConf.getHostname());
      if (hc != null) {
         InterfaceConfiguration trusted = hc.getTrustedInterface();
         InterfaceConfiguration untrusted = hc.getUntrustedInterface();

         if (!hostConf.equals(hc)
               || !trusted.equals(hostConf.getTrustedInterface())
               || !untrusted.equals(hostConf.getUntrustedInterface())) {
            log.info("removing the existing hostconf");
            em.remove(hc);
            InterfaceConfiguration.IfConfigPKey tpkey = new InterfaceConfiguration.IfConfigPKey();
            tpkey.setType(Type.TRUSTED);
            tpkey.setIpAddress(trusted.getIpAddress());
            InterfaceConfiguration trusted1 = em.find(
                  InterfaceConfiguration.class, tpkey);
            if (trusted1 != null) {
               em.remove(trusted1);
            }
            InterfaceConfiguration.IfConfigPKey upkey = new InterfaceConfiguration.IfConfigPKey();
            tpkey.setType(Type.UNTRUSTED);
            tpkey.setIpAddress(untrusted.getIpAddress());
            InterfaceConfiguration untrusted1 = em.find(
                  InterfaceConfiguration.class, upkey);
            if (untrusted1 != null) {
               em.remove(untrusted1);
            }
            log.info("now adding the new hostconf: " + hostConf);
            em.merge(hostConf);
         } else {
            log.info("hostConf and existing hostConf are equal");
         }
      }
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.