我有一个辅助的独立应用程序,用于从一些表中加载实体的值,但由于Hibernate试图在不指定键值的情况下创建记录(即使它被指定为@GeneratedValue(strategy=GenerationType.AUTO)
),它正在崩溃。
bean是:
@Entity
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
// startRegion attributes
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id = 0;
@ManyToOne(fetch = FetchType.LAZY)
private Location parent = null;
@Column(nullable=false)
private boolean active = false;
@OneToMany(mappedBy="parent", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<Location> divisions = null;
@OneToMany(mappedBy="location", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@MapKey(name="localeId")
private Map<String, Location_i18n> descriptions = new HashMap<String, Location_i18n>();
@Column(nullable=true, length=150)
private String path = null;
@Column(nullable=true, length=20)
private String prismaCode = null;
@Column(nullable=true, length=5)
private String shortCode = null;
}
Hibernate日志中显示的SQL缺少id
字段:
insert into Location (active, parent_id, path, prismaCode, shortCode) values (?, ?, ?, ?, ?)
这显然会导致违反约束的异常。
来自persistence.xml
的相关属性
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
更让我沮丧的是,我在之前的过程中执行了一个有点类似的过程,但我找不到任何导致问题的差异。
我在MSSQL2008Express上尝试了Hibernate 3.6.10和4.1.8。
有什么想法吗?提前感谢。
发布于 2013-01-02 14:46:16
它不起作用,因为数据库的自动策略在于依赖数据库生成in。表的ID列应为自动增量列,否则应更改ID生成策略。
发布于 2018-06-26 09:12:43
您已通过输入select query手动创建表,因此hibernate不知道生成的顺序按名称hibernate_sequence
创建序列
CREATE SEQUENCE hibernate_sequence
start with 1
increment by 1
minvalue your_min_value
maxvalue your_max_value
cycle;
发布于 2013-01-02 15:18:36
如果你使用的是MsSQL,你应该把你的所有注解放在getter方法之上...有时hibernate会忽略上面定义的变量注释(主要是it MsSQL)……
https://stackoverflow.com/questions/14123921
复制相似问题