Java 休眠-无法从父对象中删除子对象
我有一个映射到应用程序中父对象的子对象列表。现在,当用户从UI中删除任何子条目时,我必须删除子对象和父对象关联,并从数据库中删除子记录。我的代码中的一切似乎都很好,但代码既不删除父子关联,也不从数据库中删除子记录。下面是我的代码: 服务代码:Java 休眠-无法从父对象中删除子对象,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我有一个映射到应用程序中父对象的子对象列表。现在,当用户从UI中删除任何子条目时,我必须删除子对象和父对象关联,并从数据库中删除子记录。我的代码中的一切似乎都很好,但代码既不删除父子关联,也不从数据库中删除子记录。下面是我的代码: 服务代码: List<ServerConfig> existingConfig = serverMasterService.getServerConfigForServer(serverId); Set<ServerConfi
List<ServerConfig> existingConfig = serverMasterService.getServerConfigForServer(serverId);
Set<ServerConfig> temp = new HashSet<ServerConfig>();
for (ServerConfig sConfig : serverMstr.getServerConfigs()) {
for (ServerConfig eConfig : existingConfig){
if(sConfig.getAttributeId() == eConfig.getAttributeId()){
sConfig.setConfigId(eConfig.getConfigId());
}else{
temp.add(sConfig);
}
}
sConfig.setServer(serverMstr);
}
serverMstr.getServerConfigs().removeAll(temp);
}
this.serverMasterService.saveServerMasters(serverMstr);
public void saveServerMasters(ServerMstr serverMstr) {
hibernateTemplate.saveOrUpdate(serverMstr);
}
<set name="serverConfigs" inverse="true" cascade="all" lazy="false">
<key>
<column name="serverId" not-null="true"/>
</key>
<one-to-many class="com.serverApp.business.model.ServerConfig"/>
</set>
<many-to-one name="server" class="com.serverApp.business.model.ServerMstr"
cascade="all" lazy="false" foreign-key="FK_SERVERMSTR" >
<column name="ServerId" not-null="true"/>
</many-to-one>
package com.serverApp.business.model;
import java.util.Set;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerMaster")
@JsonIgnoreProperties({"environmentMstr"})
public class ServerMstr {
@JsonProperty("ID")
private Long id;
@JsonProperty("ServerConfigs")
private Set<ServerConfig> serverConfigs;
public Set<ServerConfig> getServerConfigs() {
return serverConfigs;
}
public void setServerConfigs(Set<ServerConfig> serverConfigs) {
this.serverConfigs = serverConfigs;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonProperty("Name")
private String name;
@JsonProperty("Notes")
private String notes;
@JsonProperty("Location")
private String location;
@JsonProperty("SerialNo")
private String serialNo;
@JsonProperty("ServerFunction")
private String serverFunction;
@JsonProperty("ServerType")
private String serverType;
@JsonProperty("PrimAppl")
private String primAppl;
@JsonProperty("Status")
private String status;
@JsonProperty("IPAddress")
private String ipAddr;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public String getServerFunction() {
return serverFunction;
}
public void setServerFunction(String serverFunction) {
this.serverFunction = serverFunction;
}
public String getServerType() {
return serverType;
}
public void setServerType(String serverType) {
this.serverType = serverType;
}
public String getPrimAppl() {
return primAppl;
}
public void setPrimAppl(String primAppl) {
this.primAppl = primAppl;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ServerMstr(){
}
public ServerMstr( String name, String notes ){
this.name = name;
this.notes=notes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ServerMstr [id=" + id + ", serverConfigs=" + serverConfigs
+ ", name=" + name + ", notes=" + notes + ", location="
+ location + ", serialNo=" + serialNo + ", serverFunction="
+ serverFunction + ", serverType=" + serverType + ", primAppl="
+ primAppl + ", status=" + status + ", ipAddr=" + ipAddr + "]";
}
}
package com.serverApp.business.model;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerConfig")
@JsonIgnoreProperties({"configId", "server"})
public class ServerConfig {
@JsonIgnore
private ServerMstr server;
private Long configId;
@Override
public String toString() {
return "ServerConfig [server=" + server.getName() + ", configId=" + configId
+ ", attributeId=" + attributeId + ", value=" + value
+ ", serverId=" + serverId + "]";
}
@JsonProperty("AttributeId")
private Long attributeId;
@JsonProperty("Value")
private String value;
@JsonProperty("ServerId")
private Long serverId;
public Long getConfigId() {
return configId;
}
public void setConfigId(Long configId) {
this.configId = configId;
}
public Long getServerId() {
return serverId;
}
public void setServerId(Long serverId) {
this.serverId = serverId;
}
public Long getAttributeId() {
return attributeId;
}
public void setAttributeId(Long attributeId) {
this.attributeId = attributeId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ServerConfig() {
}
public ServerConfig( Long serverId, Long attributeId, String value )
{
this.serverId = serverId;
this.attributeId = attributeId;
this.value=value;
}
public void setServer(ServerMstr server) {
this.server = server;
}
public ServerMstr getServer() {
return server;
}
}
package com.serverApp.business.dao;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.serverApp.business.model.ServerConfig;
import com.serverApp.business.model.ServerMstr;
@Repository ("serverMstrDao")
public class ServerMstrDaoImpl extends AbstractDAOImpl implements ServerMstrDao{
private static final Logger logger = Logger.getLogger(ServerMstrDaoImpl.class);
/*
* This Method will be used to retrieve the list of ServerMstr Class Details from the DB
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#listServerMstrs()
*/
@Override
@SuppressWarnings({"unchecked" })
public List<ServerMstr> listServerMstrs() {
if (logger.isDebugEnabled()) {
logger.debug("Inside listServerMstrs() ");
}
return (List<ServerMstr>)hibernateTemplate.find("from ServerMstr");
}
/*
* This Method will be used to save new server to database
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#saveServerMasters(com.serverApp.business.model.ServerMstr)
*/
@Override
@Transactional
public void saveServerMasters(ServerMstr serverMstr) {
if (logger.isDebugEnabled()) {
logger.debug("Inside saveServerMasters() ");
}
System.out.println("DAO Print: "+serverMstr.toString());
hibernateTemplate.saveOrUpdate(serverMstr);
}
/*
* This Method will be used to retrieve the ServerMstr Class Details from the DB for the given name
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerByName()
*/
@SuppressWarnings("unchecked")
@Override
public ServerMstr getServerByName(String name) {
if (logger.isDebugEnabled()) {
logger.debug("fetching server from database with name : " + name );
}
DetachedCriteria criteria = DetachedCriteria.forClass(ServerMstr.class);
criteria.add(Restrictions.eq("name", name));
List<ServerMstr> serverMstr = hibernateTemplate.findByCriteria(criteria);
if(serverMstr.size() != 1)
{
logger.error("Multiple or 0 row returned for selection Server name: " + name );
return null;
}
return (ServerMstr)serverMstr.get(0);
}
/*
* This Method will be used to retrieve the ServerConfig Class List from the DB for the given server id
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerConfigForServer()
*/
@SuppressWarnings("unchecked")
@Override
public List<ServerConfig> getServerConfigForServer(Long serverId) {
if (logger.isDebugEnabled()) {
logger.debug("fetching ServerConfig list from database for server with id : " + serverId );
}
return hibernateTemplate.find("from ServerConfig where serverId = ?",serverId);
}
}
父HBM文件:
List<ServerConfig> existingConfig = serverMasterService.getServerConfigForServer(serverId);
Set<ServerConfig> temp = new HashSet<ServerConfig>();
for (ServerConfig sConfig : serverMstr.getServerConfigs()) {
for (ServerConfig eConfig : existingConfig){
if(sConfig.getAttributeId() == eConfig.getAttributeId()){
sConfig.setConfigId(eConfig.getConfigId());
}else{
temp.add(sConfig);
}
}
sConfig.setServer(serverMstr);
}
serverMstr.getServerConfigs().removeAll(temp);
}
this.serverMasterService.saveServerMasters(serverMstr);
public void saveServerMasters(ServerMstr serverMstr) {
hibernateTemplate.saveOrUpdate(serverMstr);
}
<set name="serverConfigs" inverse="true" cascade="all" lazy="false">
<key>
<column name="serverId" not-null="true"/>
</key>
<one-to-many class="com.serverApp.business.model.ServerConfig"/>
</set>
<many-to-one name="server" class="com.serverApp.business.model.ServerMstr"
cascade="all" lazy="false" foreign-key="FK_SERVERMSTR" >
<column name="ServerId" not-null="true"/>
</many-to-one>
package com.serverApp.business.model;
import java.util.Set;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerMaster")
@JsonIgnoreProperties({"environmentMstr"})
public class ServerMstr {
@JsonProperty("ID")
private Long id;
@JsonProperty("ServerConfigs")
private Set<ServerConfig> serverConfigs;
public Set<ServerConfig> getServerConfigs() {
return serverConfigs;
}
public void setServerConfigs(Set<ServerConfig> serverConfigs) {
this.serverConfigs = serverConfigs;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonProperty("Name")
private String name;
@JsonProperty("Notes")
private String notes;
@JsonProperty("Location")
private String location;
@JsonProperty("SerialNo")
private String serialNo;
@JsonProperty("ServerFunction")
private String serverFunction;
@JsonProperty("ServerType")
private String serverType;
@JsonProperty("PrimAppl")
private String primAppl;
@JsonProperty("Status")
private String status;
@JsonProperty("IPAddress")
private String ipAddr;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public String getServerFunction() {
return serverFunction;
}
public void setServerFunction(String serverFunction) {
this.serverFunction = serverFunction;
}
public String getServerType() {
return serverType;
}
public void setServerType(String serverType) {
this.serverType = serverType;
}
public String getPrimAppl() {
return primAppl;
}
public void setPrimAppl(String primAppl) {
this.primAppl = primAppl;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ServerMstr(){
}
public ServerMstr( String name, String notes ){
this.name = name;
this.notes=notes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ServerMstr [id=" + id + ", serverConfigs=" + serverConfigs
+ ", name=" + name + ", notes=" + notes + ", location="
+ location + ", serialNo=" + serialNo + ", serverFunction="
+ serverFunction + ", serverType=" + serverType + ", primAppl="
+ primAppl + ", status=" + status + ", ipAddr=" + ipAddr + "]";
}
}
package com.serverApp.business.model;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerConfig")
@JsonIgnoreProperties({"configId", "server"})
public class ServerConfig {
@JsonIgnore
private ServerMstr server;
private Long configId;
@Override
public String toString() {
return "ServerConfig [server=" + server.getName() + ", configId=" + configId
+ ", attributeId=" + attributeId + ", value=" + value
+ ", serverId=" + serverId + "]";
}
@JsonProperty("AttributeId")
private Long attributeId;
@JsonProperty("Value")
private String value;
@JsonProperty("ServerId")
private Long serverId;
public Long getConfigId() {
return configId;
}
public void setConfigId(Long configId) {
this.configId = configId;
}
public Long getServerId() {
return serverId;
}
public void setServerId(Long serverId) {
this.serverId = serverId;
}
public Long getAttributeId() {
return attributeId;
}
public void setAttributeId(Long attributeId) {
this.attributeId = attributeId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ServerConfig() {
}
public ServerConfig( Long serverId, Long attributeId, String value )
{
this.serverId = serverId;
this.attributeId = attributeId;
this.value=value;
}
public void setServer(ServerMstr server) {
this.server = server;
}
public ServerMstr getServer() {
return server;
}
}
package com.serverApp.business.dao;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.serverApp.business.model.ServerConfig;
import com.serverApp.business.model.ServerMstr;
@Repository ("serverMstrDao")
public class ServerMstrDaoImpl extends AbstractDAOImpl implements ServerMstrDao{
private static final Logger logger = Logger.getLogger(ServerMstrDaoImpl.class);
/*
* This Method will be used to retrieve the list of ServerMstr Class Details from the DB
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#listServerMstrs()
*/
@Override
@SuppressWarnings({"unchecked" })
public List<ServerMstr> listServerMstrs() {
if (logger.isDebugEnabled()) {
logger.debug("Inside listServerMstrs() ");
}
return (List<ServerMstr>)hibernateTemplate.find("from ServerMstr");
}
/*
* This Method will be used to save new server to database
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#saveServerMasters(com.serverApp.business.model.ServerMstr)
*/
@Override
@Transactional
public void saveServerMasters(ServerMstr serverMstr) {
if (logger.isDebugEnabled()) {
logger.debug("Inside saveServerMasters() ");
}
System.out.println("DAO Print: "+serverMstr.toString());
hibernateTemplate.saveOrUpdate(serverMstr);
}
/*
* This Method will be used to retrieve the ServerMstr Class Details from the DB for the given name
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerByName()
*/
@SuppressWarnings("unchecked")
@Override
public ServerMstr getServerByName(String name) {
if (logger.isDebugEnabled()) {
logger.debug("fetching server from database with name : " + name );
}
DetachedCriteria criteria = DetachedCriteria.forClass(ServerMstr.class);
criteria.add(Restrictions.eq("name", name));
List<ServerMstr> serverMstr = hibernateTemplate.findByCriteria(criteria);
if(serverMstr.size() != 1)
{
logger.error("Multiple or 0 row returned for selection Server name: " + name );
return null;
}
return (ServerMstr)serverMstr.get(0);
}
/*
* This Method will be used to retrieve the ServerConfig Class List from the DB for the given server id
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerConfigForServer()
*/
@SuppressWarnings("unchecked")
@Override
public List<ServerConfig> getServerConfigForServer(Long serverId) {
if (logger.isDebugEnabled()) {
logger.debug("fetching ServerConfig list from database for server with id : " + serverId );
}
return hibernateTemplate.find("from ServerConfig where serverId = ?",serverId);
}
}
编辑
父对象类:
List<ServerConfig> existingConfig = serverMasterService.getServerConfigForServer(serverId);
Set<ServerConfig> temp = new HashSet<ServerConfig>();
for (ServerConfig sConfig : serverMstr.getServerConfigs()) {
for (ServerConfig eConfig : existingConfig){
if(sConfig.getAttributeId() == eConfig.getAttributeId()){
sConfig.setConfigId(eConfig.getConfigId());
}else{
temp.add(sConfig);
}
}
sConfig.setServer(serverMstr);
}
serverMstr.getServerConfigs().removeAll(temp);
}
this.serverMasterService.saveServerMasters(serverMstr);
public void saveServerMasters(ServerMstr serverMstr) {
hibernateTemplate.saveOrUpdate(serverMstr);
}
<set name="serverConfigs" inverse="true" cascade="all" lazy="false">
<key>
<column name="serverId" not-null="true"/>
</key>
<one-to-many class="com.serverApp.business.model.ServerConfig"/>
</set>
<many-to-one name="server" class="com.serverApp.business.model.ServerMstr"
cascade="all" lazy="false" foreign-key="FK_SERVERMSTR" >
<column name="ServerId" not-null="true"/>
</many-to-one>
package com.serverApp.business.model;
import java.util.Set;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerMaster")
@JsonIgnoreProperties({"environmentMstr"})
public class ServerMstr {
@JsonProperty("ID")
private Long id;
@JsonProperty("ServerConfigs")
private Set<ServerConfig> serverConfigs;
public Set<ServerConfig> getServerConfigs() {
return serverConfigs;
}
public void setServerConfigs(Set<ServerConfig> serverConfigs) {
this.serverConfigs = serverConfigs;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonProperty("Name")
private String name;
@JsonProperty("Notes")
private String notes;
@JsonProperty("Location")
private String location;
@JsonProperty("SerialNo")
private String serialNo;
@JsonProperty("ServerFunction")
private String serverFunction;
@JsonProperty("ServerType")
private String serverType;
@JsonProperty("PrimAppl")
private String primAppl;
@JsonProperty("Status")
private String status;
@JsonProperty("IPAddress")
private String ipAddr;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public String getServerFunction() {
return serverFunction;
}
public void setServerFunction(String serverFunction) {
this.serverFunction = serverFunction;
}
public String getServerType() {
return serverType;
}
public void setServerType(String serverType) {
this.serverType = serverType;
}
public String getPrimAppl() {
return primAppl;
}
public void setPrimAppl(String primAppl) {
this.primAppl = primAppl;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ServerMstr(){
}
public ServerMstr( String name, String notes ){
this.name = name;
this.notes=notes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ServerMstr [id=" + id + ", serverConfigs=" + serverConfigs
+ ", name=" + name + ", notes=" + notes + ", location="
+ location + ", serialNo=" + serialNo + ", serverFunction="
+ serverFunction + ", serverType=" + serverType + ", primAppl="
+ primAppl + ", status=" + status + ", ipAddr=" + ipAddr + "]";
}
}
package com.serverApp.business.model;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerConfig")
@JsonIgnoreProperties({"configId", "server"})
public class ServerConfig {
@JsonIgnore
private ServerMstr server;
private Long configId;
@Override
public String toString() {
return "ServerConfig [server=" + server.getName() + ", configId=" + configId
+ ", attributeId=" + attributeId + ", value=" + value
+ ", serverId=" + serverId + "]";
}
@JsonProperty("AttributeId")
private Long attributeId;
@JsonProperty("Value")
private String value;
@JsonProperty("ServerId")
private Long serverId;
public Long getConfigId() {
return configId;
}
public void setConfigId(Long configId) {
this.configId = configId;
}
public Long getServerId() {
return serverId;
}
public void setServerId(Long serverId) {
this.serverId = serverId;
}
public Long getAttributeId() {
return attributeId;
}
public void setAttributeId(Long attributeId) {
this.attributeId = attributeId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ServerConfig() {
}
public ServerConfig( Long serverId, Long attributeId, String value )
{
this.serverId = serverId;
this.attributeId = attributeId;
this.value=value;
}
public void setServer(ServerMstr server) {
this.server = server;
}
public ServerMstr getServer() {
return server;
}
}
package com.serverApp.business.dao;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.serverApp.business.model.ServerConfig;
import com.serverApp.business.model.ServerMstr;
@Repository ("serverMstrDao")
public class ServerMstrDaoImpl extends AbstractDAOImpl implements ServerMstrDao{
private static final Logger logger = Logger.getLogger(ServerMstrDaoImpl.class);
/*
* This Method will be used to retrieve the list of ServerMstr Class Details from the DB
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#listServerMstrs()
*/
@Override
@SuppressWarnings({"unchecked" })
public List<ServerMstr> listServerMstrs() {
if (logger.isDebugEnabled()) {
logger.debug("Inside listServerMstrs() ");
}
return (List<ServerMstr>)hibernateTemplate.find("from ServerMstr");
}
/*
* This Method will be used to save new server to database
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#saveServerMasters(com.serverApp.business.model.ServerMstr)
*/
@Override
@Transactional
public void saveServerMasters(ServerMstr serverMstr) {
if (logger.isDebugEnabled()) {
logger.debug("Inside saveServerMasters() ");
}
System.out.println("DAO Print: "+serverMstr.toString());
hibernateTemplate.saveOrUpdate(serverMstr);
}
/*
* This Method will be used to retrieve the ServerMstr Class Details from the DB for the given name
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerByName()
*/
@SuppressWarnings("unchecked")
@Override
public ServerMstr getServerByName(String name) {
if (logger.isDebugEnabled()) {
logger.debug("fetching server from database with name : " + name );
}
DetachedCriteria criteria = DetachedCriteria.forClass(ServerMstr.class);
criteria.add(Restrictions.eq("name", name));
List<ServerMstr> serverMstr = hibernateTemplate.findByCriteria(criteria);
if(serverMstr.size() != 1)
{
logger.error("Multiple or 0 row returned for selection Server name: " + name );
return null;
}
return (ServerMstr)serverMstr.get(0);
}
/*
* This Method will be used to retrieve the ServerConfig Class List from the DB for the given server id
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerConfigForServer()
*/
@SuppressWarnings("unchecked")
@Override
public List<ServerConfig> getServerConfigForServer(Long serverId) {
if (logger.isDebugEnabled()) {
logger.debug("fetching ServerConfig list from database for server with id : " + serverId );
}
return hibernateTemplate.find("from ServerConfig where serverId = ?",serverId);
}
}
DAO代码:
List<ServerConfig> existingConfig = serverMasterService.getServerConfigForServer(serverId);
Set<ServerConfig> temp = new HashSet<ServerConfig>();
for (ServerConfig sConfig : serverMstr.getServerConfigs()) {
for (ServerConfig eConfig : existingConfig){
if(sConfig.getAttributeId() == eConfig.getAttributeId()){
sConfig.setConfigId(eConfig.getConfigId());
}else{
temp.add(sConfig);
}
}
sConfig.setServer(serverMstr);
}
serverMstr.getServerConfigs().removeAll(temp);
}
this.serverMasterService.saveServerMasters(serverMstr);
public void saveServerMasters(ServerMstr serverMstr) {
hibernateTemplate.saveOrUpdate(serverMstr);
}
<set name="serverConfigs" inverse="true" cascade="all" lazy="false">
<key>
<column name="serverId" not-null="true"/>
</key>
<one-to-many class="com.serverApp.business.model.ServerConfig"/>
</set>
<many-to-one name="server" class="com.serverApp.business.model.ServerMstr"
cascade="all" lazy="false" foreign-key="FK_SERVERMSTR" >
<column name="ServerId" not-null="true"/>
</many-to-one>
package com.serverApp.business.model;
import java.util.Set;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerMaster")
@JsonIgnoreProperties({"environmentMstr"})
public class ServerMstr {
@JsonProperty("ID")
private Long id;
@JsonProperty("ServerConfigs")
private Set<ServerConfig> serverConfigs;
public Set<ServerConfig> getServerConfigs() {
return serverConfigs;
}
public void setServerConfigs(Set<ServerConfig> serverConfigs) {
this.serverConfigs = serverConfigs;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonProperty("Name")
private String name;
@JsonProperty("Notes")
private String notes;
@JsonProperty("Location")
private String location;
@JsonProperty("SerialNo")
private String serialNo;
@JsonProperty("ServerFunction")
private String serverFunction;
@JsonProperty("ServerType")
private String serverType;
@JsonProperty("PrimAppl")
private String primAppl;
@JsonProperty("Status")
private String status;
@JsonProperty("IPAddress")
private String ipAddr;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getSerialNo() {
return serialNo;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public String getServerFunction() {
return serverFunction;
}
public void setServerFunction(String serverFunction) {
this.serverFunction = serverFunction;
}
public String getServerType() {
return serverType;
}
public void setServerType(String serverType) {
this.serverType = serverType;
}
public String getPrimAppl() {
return primAppl;
}
public void setPrimAppl(String primAppl) {
this.primAppl = primAppl;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ServerMstr(){
}
public ServerMstr( String name, String notes ){
this.name = name;
this.notes=notes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ServerMstr [id=" + id + ", serverConfigs=" + serverConfigs
+ ", name=" + name + ", notes=" + notes + ", location="
+ location + ", serialNo=" + serialNo + ", serverFunction="
+ serverFunction + ", serverType=" + serverType + ", primAppl="
+ primAppl + ", status=" + status + ", ipAddr=" + ipAddr + "]";
}
}
package com.serverApp.business.model;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement(name = "ServerConfig")
@JsonIgnoreProperties({"configId", "server"})
public class ServerConfig {
@JsonIgnore
private ServerMstr server;
private Long configId;
@Override
public String toString() {
return "ServerConfig [server=" + server.getName() + ", configId=" + configId
+ ", attributeId=" + attributeId + ", value=" + value
+ ", serverId=" + serverId + "]";
}
@JsonProperty("AttributeId")
private Long attributeId;
@JsonProperty("Value")
private String value;
@JsonProperty("ServerId")
private Long serverId;
public Long getConfigId() {
return configId;
}
public void setConfigId(Long configId) {
this.configId = configId;
}
public Long getServerId() {
return serverId;
}
public void setServerId(Long serverId) {
this.serverId = serverId;
}
public Long getAttributeId() {
return attributeId;
}
public void setAttributeId(Long attributeId) {
this.attributeId = attributeId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public ServerConfig() {
}
public ServerConfig( Long serverId, Long attributeId, String value )
{
this.serverId = serverId;
this.attributeId = attributeId;
this.value=value;
}
public void setServer(ServerMstr server) {
this.server = server;
}
public ServerMstr getServer() {
return server;
}
}
package com.serverApp.business.dao;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.serverApp.business.model.ServerConfig;
import com.serverApp.business.model.ServerMstr;
@Repository ("serverMstrDao")
public class ServerMstrDaoImpl extends AbstractDAOImpl implements ServerMstrDao{
private static final Logger logger = Logger.getLogger(ServerMstrDaoImpl.class);
/*
* This Method will be used to retrieve the list of ServerMstr Class Details from the DB
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#listServerMstrs()
*/
@Override
@SuppressWarnings({"unchecked" })
public List<ServerMstr> listServerMstrs() {
if (logger.isDebugEnabled()) {
logger.debug("Inside listServerMstrs() ");
}
return (List<ServerMstr>)hibernateTemplate.find("from ServerMstr");
}
/*
* This Method will be used to save new server to database
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#saveServerMasters(com.serverApp.business.model.ServerMstr)
*/
@Override
@Transactional
public void saveServerMasters(ServerMstr serverMstr) {
if (logger.isDebugEnabled()) {
logger.debug("Inside saveServerMasters() ");
}
System.out.println("DAO Print: "+serverMstr.toString());
hibernateTemplate.saveOrUpdate(serverMstr);
}
/*
* This Method will be used to retrieve the ServerMstr Class Details from the DB for the given name
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerByName()
*/
@SuppressWarnings("unchecked")
@Override
public ServerMstr getServerByName(String name) {
if (logger.isDebugEnabled()) {
logger.debug("fetching server from database with name : " + name );
}
DetachedCriteria criteria = DetachedCriteria.forClass(ServerMstr.class);
criteria.add(Restrictions.eq("name", name));
List<ServerMstr> serverMstr = hibernateTemplate.findByCriteria(criteria);
if(serverMstr.size() != 1)
{
logger.error("Multiple or 0 row returned for selection Server name: " + name );
return null;
}
return (ServerMstr)serverMstr.get(0);
}
/*
* This Method will be used to retrieve the ServerConfig Class List from the DB for the given server id
* (non-Javadoc)
* @see com.serverApp.business.dao.ServerMstrDao#getServerConfigForServer()
*/
@SuppressWarnings("unchecked")
@Override
public List<ServerConfig> getServerConfigForServer(Long serverId) {
if (logger.isDebugEnabled()) {
logger.debug("fetching ServerConfig list from database for server with id : " + serverId );
}
return hibernateTemplate.find("from ServerConfig where serverId = ?",serverId);
}
}
package com.serverApp.business.dao;
导入java.util.List;
导入org.apache.log4j.Logger;
导入org.hibernate.criteria.DetachedCriteria;
导入org.hibernate.criteria.Restrictions;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.Transactional;
导入com.serverApp.business.model.ServerConfig;
导入com.serverApp.business.model.ServerMstr;
@存储库(“serverMstrDao”)
公共类ServerMstrDaoImpl扩展了AbstractDAOImpl,实现了ServerMstrDao{
私有静态最终记录器Logger=Logger.getLogger(ServerMstrDaoImpl.class);
/*
*此方法将用于从数据库检索ServerMstr类详细信息列表
*(非Javadoc)
*@see com.serverApp.business.dao.ServerMstrDao#listServerMstrs()
*/
@凌驾
@SuppressWarnings({“unchecked”})
公共列表listServerMstrs(){
if(logger.isDebugEnabled()){
debug(“内部listServerMstrs()”;
}
return(List)hibernateTemplate.find(“来自ServerMstr”);
}
/*
*此方法将用于将新服务器保存到数据库
*(非Javadoc)
*@see com.serverApp.business.dao.ServerMstrDao#saveServerMasters(com.serverApp.business.model.ServerMstr)
*/
@凌驾
@交易的
public void saveServerMasters(ServerMstr ServerMstr){
if(logger.isDebugEnabled()){
debug(“内部saveServerMasters()”;
}
System.out.println(“DAO Print:+serverMstr.toString());
hibernateTemplate.saveOrUpdate(serverMstr);
}
/*
*此方法将用于从数据库中检索给定名称的ServerMstr类详细信息
*(非Javadoc)
*@see com.serverApp.business.dao.ServerMstrDao#getServerByName()
*/
@抑制警告(“未选中”)
@凌驾
公共服务器MSTR getServerByName(字符串名称){
if(logger.isDebugEnabled()){
debug(“正在从名为:“+name”的数据库中获取服务器);
}
DetachedCriteria=DetachedCriteria.forClass(ServerMstr.class);
标准。添加(限制。等式(“名称”),名称);
List serverMstr=hibernateTemplate.findByCriteria(标准);
如果(serverMstr.size()!=1)
{
logger.error(“选择服务器名称:“+name”返回多行或0行);
返回null;
}
return(ServerMstr)ServerMstr.get(0);
}
/*
*此方法将用于从数据库中检索给定服务器id的ServerConfig类列表
*(非Javadoc)
*@see com.serverApp.business.dao.ServerMstrDao#getServerConfigForServer()
*/
@抑制警告(“未选中”)
@凌驾
公共列表getServerConfigForServer(长服务器ID){
if(logger.isDebugEnabled()){
debug(“正在从数据库中获取id为“+serverId”的服务器的ServerConfig列表);
}
返回hibernateTemplate.find(“从ServerConfig,其中serverId=?”,serverId);
}
}
非常感谢您的帮助/建议在这种情况下,您需要的是删除孤儿。级联类型的
all
确实级联了所有操作,但是要在子对象上执行删除操作,您需要在父对象上执行删除操作,这不是您在示例中希望执行的操作。按如下方式更改映射:
<set name="serverConfigs" inverse="true" cascade="all-delete-orphan" lazy="false">
<key>
<column name="serverId" not-null="true"/>
</key>
<one-to-many class="com.serverApp.business.model.ServerConfig"/>
</set>
在这种情况下,您需要删除孤立项。级联类型的
all
确实级联了所有操作,但是要在子对象上执行删除操作,您需要在父对象上执行删除操作,这不是您在示例中希望执行的操作。按如下方式更改映射:
<set name="serverConfigs" inverse="true" cascade="all-delete-orphan" lazy="false">
<key>
<column name="serverId" not-null="true"/>
</key>
<one-to-many class="com.serverApp.business.model.ServerConfig"/>
</set>
您需要重写POJO对象中的equals和haschode方法。如果不这样做,Hibernate将无法比较子对象,它们将继续作为孤立对象存在于数据库中 另一个选项是查找父对象的所有子对象,然后按照以下代码分别删除它们:
Iterator<Child> i = form.getDeleteItems().iterator();
while(i.hasNext()){
Child child = i.next();
for (Iterator<Child> it = parent.getChildren().iterator();) {
if (child.getId().equals(it.next().getId()) {
it.remove();
}
}
}
Iterator i=form.getDeleteItems().Iterator();
while(i.hasNext()){
Child=i.next();
for(Iterator it=parent.getChildren().Iterator();){
if(child.getId().equals(it.next().getId()){
it.remove();
}
}
}
您需要覆盖POJO对象中的equals和haschode方法。如果不这样做,Hibernate将无法比较子对象,它们将继续作为孤立对象存在于数据库中
另一个选项是查找父对象的所有子对象,然后按照以下代码分别删除它们:
Iterator<Child> i = form.getDeleteItems().iterator();
while(i.hasNext()){
Child child = i.next();
for (Iterator<Child> it = parent.getChildren().iterator();) {
if (child.getId().equals(it.next().getId()) {
it.remove();
}
}
}
Iterator i=form.getDeleteItems().Iterator();
while(i.hasNext()){
Child=i.next();
for(Iterator it=parent.getChildren().Iterator();){
if(child.getId().equals(it.next().getId()){
it.remove();
}
}
}
这是正确答案。有关更多信息,请参阅此SO链接中的图表:。图表的第三行显示,要在修改父对象集合时删除子对象,必须使用“全部删除孤立对象”选项。如果使用批注而不是基于XML的配置,则可以在例如@OneToMany中使用orphanRemove=true标记。@KyleM链接中的答案提供了一个很好的总结行为的方法,谢谢。我尝试将cascade
类型与@Pritam Banerjee的解决方案一起更改为all delete orphan
,但没有成功。请修改