我的新项目是在Hibernate4.2.5.Final和Spring。登录后,我将用户对象存储在会话中。现在成功登录后,我需要在应用程序日志中插入一条记录。以下是类:
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long ID;
@Version
private Long version;
private Long createdBy;
@Temporal(value = TemporalType.TIMESTAMP)
private Date createdDate;
private Long updatedBy;
@Temporal(value = TemporalType.TIMESTAMP)
private Date updatedDate;
private String deactivatedReason;
private String activatedReason;
private Integer active;
public long getID() {
return ID;
}
public void setID(long iD) {
ID = iD;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Long getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Long createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Long getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(Long updatedBy) {
this.updatedBy = updatedBy;
}
public Date getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(Date updatedDate) {
this.updatedDate = updatedDate;
}
public String getDeactivatedReason() {
return deactivatedReason;
}
public void setDeactivatedReason(String deactivatedReason) {
this.deactivatedReason = deactivatedReason;
}
public String getActivatedReason() {
return activatedReason;
}
public void setActivatedReason(String activatedReason) {
this.activatedReason = activatedReason;
}
public Integer getActive() {
return active;
}
public void setActive(Integer active) {
this.active = active;
}
}
package com.product.domain;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import com.product.audit.IAuditLog;
import com.product.domain.base.BaseEntity;
@Entity
@Table(name = "users")
@NamedQueries({ @NamedQuery(name = "Users.findUserByUserID", query = "SELECT usr FROM Users as usr WHERE usr.userName = ? and usr.practice.code = ?") })
public class Users extends BaseEntity implements IAuditLog {
private String userName;
private String password;
// bi-directional many-to-one association to Practice
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "practiceID")
private Practice practice;
//getters and setters
}
package com.product.domain;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "applicationevents")
public class ApplicationEvents {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long ID;
@Temporal(value = TemporalType.TIMESTAMP)
private Date eventDate;
private String comments;
private Integer eventType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "practiceID")
private Practice practice;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userID")
private Users userID;
//getters and setters
}
ApplicationEvents appEvent = new ApplicationEvents();
appEvent.setEventType(eventType);
appEvent.setPractice(practice);
appEvent.setUserID(userID);
appEvent.setComments(comments);
appEvent.setEventDate(new Timestamp(new Date().getTime()));
CRUDService.Save(appEvent);
18:34:34.540 [http-bio-8080-exec-8] INFO c.p.a.MyUserDetailsService - Logged User authentication sucess. UserName admin and practice Name Physicians Choice Laboratory
Hibernate:
/* insert com.product.domain.ApplicationEvents
*/ insert
into
applicationevents
(comments, eventDate, eventType, practiceID, userID)
values
(?, ?, ?, ?, ?)
19 Sep, 2013 6:34:34 PM org.zkoss.bind.impl.ParamCall call:117
SEVERE:
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.product.domain.Users; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.product.domain.Users
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:166)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:680)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:562)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
非常重要的注意事项:当我移除基实体中的@版本时,一切工作都很好。
首先,您需要保存用户。
CRUDService.Save(userID);
然后将用户与ApplicationEvents相关联
ApplicationEvents appEvent = new ApplicationEvents();
appEvent.setEventType(eventType);
appEvent.setPractice(practice);
appEvent.setUserID(userID);
appEvent.setComments(comments);
appEvent.setEventDate(new Timestamp(new Date().getTime()));
CRUDService.Save(appEvent);
我正在使用Seam Framework。我有2个实体: Request.java 和请求事件.java 当我进行此交易时: 我得到了这个错误:
问题内容: 并(除其他事项外)这样做: 我得到提到的异常? 我需要手动调用session.save(person)吗?我必须在孩子的类定义中添加级联类型注释(它引用父类的地方)吗? 还是我错过了其他明显的东西? 我不想使用CascadeType.ALL,因为当删除父对象时,我想保留此人(孩子)。 两个实体/表都扩展了一个公共基表: 这会影响需要哪种级联类型? 问题答案: 您没有提到Hibernat
对于我的问题,我遇到了一些可能的好答案,但这是关于从Hibernate3.4.0ga升级到Hibernate4.1.8的。所以这在以前的版本下是可行的,我已经搜索了上上下下为什么它在这个新版本中被打破了。 我得到一个 这是我的课。
受雇的 Emploadable
我有一个数据库,有两个表Batchstatistics和ReconProcessDateTracker。我想了解他们之间的关系。我使用以下模型类使用hibernate实现这一点: } } 当我试图保存它时,我得到的错误对象引用了一个未保存的瞬态实例——在刷新之前保存瞬态实例:com。美国药典。实体TBLBatch统计
错误: 我还尝试了,但是 病因是什么?显然,我设置了nullable=true(在数据库中也是如此),尽管我有这个错误。有什么想法吗?