我正在使用Spring Roo基于表(在MS SQL Server上创建)生成实体。以下是我的sql脚本:
CREATE TABLE [dbo].[JMSMessage](
[MessageID] [nvarchar](100) NOT NULL,
[Destination] [varchar](50) NOT
[Status] [varchar](15) NULL,
CONSTRAINT [PK_JMSMessage] PRIMARY KEY CLUSTERED
(
[MessageID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Spring Roo生成的Java实体
@Entity
@Table(schema = "dbo",name = "JMSMessage")
@Configurable
public class Jmsmessage {
@Column(name = "Destination", length = 50)
@NotNull
private String destination;
@Column(name = "Status", length = 15)
private String status;
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "MessageID", length = 100)
private String messageId;
public String getMessageId() {
return this.messageId;
}
public void setMessageId(String id) {
this.messageId = id;
}
@PersistenceContext
transient EntityManager entityManager;
public static final EntityManager entityManager() {
EntityManager em = new Jmsmessage().entityManager;
if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
return em;
}
public static Jmsmessage findJmsmessage(String messageId) {
if (messageId == null || messageId.length() == 0) return null;
return entityManager().find(Jmsmessage.class, messageId);
}
@Transactional
public void persist() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.persist(this);
}
@Transactional
public void remove() {
if (this.entityManager == null) this.entityManager = entityManager();
if (this.entityManager.contains(this)) {
this.entityManager.remove(this);
} else {
Jmsmessage attached = Jmsmessage.findJmsmessage(this.messageId);
this.entityManager.remove(attached);
}
}
@Transactional
public void flush() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.flush();
}
@Transactional
public void clear() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.clear();
}
@Transactional
public Jmsmessage merge() {
if (this.entityManager == null) this.entityManager = entityManager();
Jmsmessage merged = this.entityManager.merge(this);
this.entityManager.flush();
return merged;
}
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
然后,当我运行以下代码来创建实体实例并尝试持久化时,程序抛出一个PersistentObjectException,说“传递给持久化的分离实体”。
Jmsmessage message = new Jmsmessage();
message.setMessageId("m1");
message.setDestination("queue1");
message.setStatus("status1");
message.persist(); // If i use message.merge(), it will throws another Sql exception saying "Cannot insert the value NULL into column 'MessageID'"
我如何能够坚持不懈地工作,有什么建议吗?提前谢谢!
答案 0 :(得分:3)
您不应为messageId设置值,因为您的策略为@GeneratedValue(strategy = GenerationType.AUTO)
另一方面,我注意到您使用了roo提供的活动记录模式。我建议您将此更改为DAO模式,因为这样的代码在Java / Spring / Hibernate世界中非常罕见:)
答案 1 :(得分:2)
您拥有ID生成策略自动,但您已在实体中分配了一些ID值。意思是ORM引擎会将您的新实体视为已分离的实体,因为它获得了id并且它会像尝试保存分离的实体一样抛出异常..如果您尝试合并实体以克服问题,ORM引擎将检查您提供给它的ID实体,它不能匹配任何行与您的实体匹配,因为没有这样的行与主键。
错误的底线是,您将ORM引擎与ID生成混淆。
希望这很有用!