Senhores,
Alguém vê algo óbvio nas classes abaixo para que o Hibernate faça “insert” mas não “update”, assumindo que a classe passada como parâmetro está correta com versão=0 (mesmo valor da base quando no update).
O Hibernate não gera exceção, mas também não faz update. Ele simplesmente passa pelo código sem executar o sql “update” (já vi no log).
Abraço …
Entidade:
package br.com.maps.apus.entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Version;
import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import br.com.maps.apus.entity.enumeration.MessageTypeEnum;
import br.com.maps.apus.entity.enumeration.ObligationEventEnum;
import br.com.regerbanc.infra.entity.AbstractEntityImpl;
import br.com.regerbanc.infra.entity.Entity;
/**
* Alert template.
*
* @author s.santos
*
*/
@javax.persistence.Entity
@org.hibernate.annotations.Entity(dynamicInsert = false, dynamicUpdate = false, mutable = false, optimisticLock = OptimisticLockType.VERSION)
@javax.persistence.Table(name = "TB_ALERT_MESS_TEMP")
public class AlertMessageTemplateEntity extends AbstractEntityImpl implements
Comparable<AlertMessageTemplateEntity> {
/**
* Alert message Id
*/
private Integer id;
/**
* Version lock
*/
private Integer version;
/**
* Text of the message template
*/
private String text;
/**
* Title of the message
*/
private String title;
/**
* Message type
*/
private MessageTypeEnum messageType;
/**
* Obligation event
*/
private ObligationEventEnum obligationEvent;
/**
* Company legacy id
*/
private String companyLegacyId;
/**
* Default constructor
*/
public AlertMessageTemplateEntity() {
super();
}
/**
* Constructor with fields
*
* @param id
* @param text
* @param title
* @param messageType
* @param obligationEvent
*/
public AlertMessageTemplateEntity(Integer id, String text, String title,
MessageTypeEnum messageType, ObligationEventEnum obligationEvent) {
super();
this.id = id;
this.text = text;
this.title = title;
this.messageType = messageType;
this.obligationEvent = obligationEvent;
}
@Id
@GeneratedValue(generator = "SEQ_ALERT_MESS", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "SEQ_ALERT_MESS", sequenceName = "TB_ALERT_MESS_SQ_ALERT_MES_SEQ")
@Column( name = "SQ_ALERT_MESS_TEMP")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Version
@Column(name = "NU_VERS")
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Type(type = "br.com.regerbanc.infra.entity.type.EnhancedEnumOrdinalUserType", parameters = @Parameter(name="enumClassName", value="br.com.maps.apus.entity.enumeration.MessageTypeEnum"))
@Column(name="SQ_MESS_TYPE", nullable = false)
public MessageTypeEnum getMessageType() {
return messageType;
}
public void setMessageType(MessageTypeEnum messageType) {
this.messageType = messageType;
}
@Type(type = "br.com.regerbanc.infra.entity.type.EnhancedEnumOrdinalUserType", parameters = @Parameter(name="enumClassName", value="br.com.maps.apus.entity.enumeration.ObligationEventEnum"))
@Column(name="SQ_OBLG_EVNT", nullable = false)
public ObligationEventEnum getObligationEvent() {
return obligationEvent;
}
public void setObligationEvent(ObligationEventEnum obligationEvent) {
this.obligationEvent = obligationEvent;
}
@Column(name = "DE_TEXT", length = 500)
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Column(name = "NM_TITL", length = 255)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name = "SQ_COMP_ID", nullable = false)
public String getCompanyLegacyId() {
return companyLegacyId;
}
public void setCompanyLegacyId(String companyLegacyId) {
this.companyLegacyId = companyLegacyId;
}
public int compareTo(AlertMessageTemplateEntity o) {
if (o == null)
return -1;
return getId().compareTo(o.getId());
}
@Override
protected Class<? extends Entity> doGetClassForEquals() {
return AlertMessageTemplateEntity.class;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyLegacyId == null) ? 0 : companyLegacyId.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
+ ((messageType == null) ? 0 : messageType.hashCode());
result = prime * result
+ ((obligationEvent == null) ? 0 : obligationEvent.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
}
@Override
public String toString() {
return "AlertMessageTemplateEntity: id=" + getId() + ", title='"
+ getTitle() + "'";
}
@Override
public boolean doEquals(Entity obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
final AlertMessageTemplateEntity other = (AlertMessageTemplateEntity) obj;
if (companyLegacyId == null) {
if (other.companyLegacyId != null)
return false;
} else if (!companyLegacyId.equals(other.companyLegacyId))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (messageType == null) {
if (other.messageType != null)
return false;
} else if (!messageType.equals(other.messageType))
return false;
if (obligationEvent == null) {
if (other.obligationEvent != null)
return false;
} else if (!obligationEvent.equals(other.obligationEvent))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
return true;
}
}
Chamada para persistir:
public AlertMessageTemplateEntity saveOrUpdate(
AlertMessageTemplateEntity template) throws BusinessException,
BusinessFailureException {
Session session = null;
try {
AlertMessageTemplateEntity editTemplate = template;
session = getHibernateService().getCurrentSession();
if ( template.getId() != null && template.getId().intValue() != 0 ) {
editTemplate = getById( template.getId() );
editTemplate.setMessageType( template.getMessageType() );
editTemplate.setObligationEvent( template.getObligationEvent() );
editTemplate.setText( template.getText() );
editTemplate.setTitle( template.getTitle() );
}
session.merge( editTemplate );
session.flush();
return template;
} catch (Exception e) {
throw new BusinessFailureException( e );
} finally {
getHibernateService().closeSession(session);
}
}