Hibernate 自动增量不起作用
我的数据库中有一个列设置为 Identity(1,1),但我无法让 hibernate 注释为其工作。当我尝试创建新记录时出现错误。
在我的实体中,我有以下内容。
@Entity
@Table(schema="dbo", name="MemberSelectedOptions")
public class MemberSelectedOption extends BampiEntity implements Serializable {
@Embeddable
public static class MSOPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="SourceApplication")
String sourceApplication;
@Column(name="GroupId")
String groupId;
@Column(name="MemberId")
String memberId;
@Column(name="OptionId")
int optionId;
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="native")
@Column(name="SeqNo", unique=true, nullable=false)
BigDecimal seqNo;
//Getters and setters here...
}
private static final long serialVersionUID = 1L;
@EmbeddedId
MSOPK pk = new MSOPK();
@Column(name="OptionStatusCd")
String optionStatusCd;
@Column(name="EffectiveDate")
Date effectiveDate;
@Column(name="TermDate")
Date termDate;
@Column(name="SelectionStatusDate")
Date selectionStatusDate;
@Column(name="SysLstUpdtUserId")
String sysLstUpdtUserId = Globals.WS_USER_ID;;
@Column(name="SysLstTrxDtm")
Date sysLstTrxDtm = new Date();
@OneToMany(mappedBy="option")
List<MemberSelectedVariable> variables =
new ArrayList<MemberSelectedVariable>();
//More Getters and setters here...
}
但是当我尝试添加新记录时,出现以下错误。
当 IDENTITY_INSERT 设置为 OFF 时,无法在表“MemberSelectedOptions”中插入标识列的显式值。我不想将 IDENTIY_INSERT 设置为 ON,因为我希望数据库中的标识列来管理值。
运行的 SQL 如下;您可以清楚地看到插入物的位置。
insert into dbo.MemberSelectedOptions
(OptionStatusCd,
EffectiveDate,
TermDate,
SelectionStatusDate,
SysLstUpdtUserId,
SysLstTrxDtm,
SourceApplication,
GroupId,
MemberId,
OptionId,
SeqNo)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
我缺少什么?
I have a column in my DB that is set with Identity(1,1) and I can't get hibernate annotations to work for it. I get errors when I try to create a new record.
In my entity I have the following.
@Entity
@Table(schema="dbo", name="MemberSelectedOptions")
public class MemberSelectedOption extends BampiEntity implements Serializable {
@Embeddable
public static class MSOPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="SourceApplication")
String sourceApplication;
@Column(name="GroupId")
String groupId;
@Column(name="MemberId")
String memberId;
@Column(name="OptionId")
int optionId;
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="native")
@Column(name="SeqNo", unique=true, nullable=false)
BigDecimal seqNo;
//Getters and setters here...
}
private static final long serialVersionUID = 1L;
@EmbeddedId
MSOPK pk = new MSOPK();
@Column(name="OptionStatusCd")
String optionStatusCd;
@Column(name="EffectiveDate")
Date effectiveDate;
@Column(name="TermDate")
Date termDate;
@Column(name="SelectionStatusDate")
Date selectionStatusDate;
@Column(name="SysLstUpdtUserId")
String sysLstUpdtUserId = Globals.WS_USER_ID;;
@Column(name="SysLstTrxDtm")
Date sysLstTrxDtm = new Date();
@OneToMany(mappedBy="option")
List<MemberSelectedVariable> variables =
new ArrayList<MemberSelectedVariable>();
//More Getters and setters here...
}
But when I try to add a new record I get the following error.
Cannot insert explicit value for identity column in table 'MemberSelectedOptions' when IDENTITY_INSERT is set to OFF. I don't want to set IDENTIY_INSERT to ON because I want the identity column in the db to manage the values.
The SQL that is run is the following; where you can clearly see the insert.
insert into dbo.MemberSelectedOptions
(OptionStatusCd,
EffectiveDate,
TermDate,
SelectionStatusDate,
SysLstUpdtUserId,
SysLstTrxDtm,
SourceApplication,
GroupId,
MemberId,
OptionId,
SeqNo)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您使用
@Embeddable
或@EmbeddedId
时,主键值应该由应用程序提供(即由非生成的值组成) )。您的@GeneratedValue
注释将被忽略。When you use
@Embeddable
or@EmbeddedId
, the primary key values are supposed to be provided by the application (i.e. made up of non generated values). Your@GeneratedValue
annotation is just ignored.这个组合对我来说非常有用:
this combination works great for me:
这是执行此操作的示例
Here is the example to do it
可能您需要使用
@id
标记您的字段,而不指定generator
属性。如 Hibernate 注释 - 2.2.3.1 中所示。生成标识符属性,下一个示例使用身份生成器:
Possible you need to mark your field with
@id
and not specifygenerator
property.As showed in Hibernate Annotation - 2.2.3.1. Generating the identifier property, the next example uses the identity generator:
您不能在复合键上使用生成器
You can't use Generators on composite keys
你不能这样做
手动创建表,一切都会好的。
You can't do it with
Create table manually and everything will be ok.