2013-01-02 211 views
0

我有一个辅助独立应用程序来加载某些表中的实体的值,它正在下降,因为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。

任何想法?预先感谢。

回答

1

它不起作用,因为数据库的AUTO策略依赖于数据库来生成ID。该表的ID列应该是自动增量列,或者您应该更改ID生成策略。

+0

好了你是对的;我正在让hibernate生成这些表,但不知怎的,'id'字段在MS SQL中没有被定义为'Identity'。放下所有的桌子,现在它的作品,谢谢。 – SJuan76

+0

对于那些通过谷歌搜索登陆这里的提示“不生成id”......我的症状相似,但根本原因不同:当ID列是Int时,Hibernate以无声和神秘的方式失败。将Int更改为Long为我修复。 –

-1

如果您使用MSSQL,你应该把你上方的getter方法的所有注解......有些时候hibernate会忽略这在上面变量(主要是MSSQL)中定义的注释......