我有一个辅助独立应用程序来加载某些表中的实体的值,它正在下降,因为Hibernate试图创建记录而不指定键值(即使它被指定为@GeneratedValue(strategy=GenerationType.AUTO)
)。JPA - >休眠不生成自动ID
这个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"/>
要添加到我的挫折,我做了一个有点类似的过程在以前的过程,但我无法找到导致问题的任何差异。
我试着用Hibernate 3.6.10和4.1.8对MS SQL 2008 Express。
任何想法?预先感谢。
好了你是对的;我正在让hibernate生成这些表,但不知怎的,'id'字段在MS SQL中没有被定义为'Identity'。放下所有的桌子,现在它的作品,谢谢。 – SJuan76
对于那些通过谷歌搜索登陆这里的提示“不生成id”......我的症状相似,但根本原因不同:当ID列是Int时,Hibernate以无声和神秘的方式失败。将Int更改为Long为我修复。 –