I have an auxiliary standalone application to load the values for entities from some tables, and it is falling because Hibernate is trying to create records without specifying the key value (even if it is specified as @GeneratedValue(strategy=GenerationType.AUTO)).
The bean is:
@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;
}
The SQL shown in Hibernate log is missing the id field:
insert into Location (active, parent_id, path, prismaCode, shortCode) values (?, ?, ?, ?, ?)
which obviously causes a constraint violation exception.
The relevant properties from 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"/>
To add to my frustrations, I did a somewhat similar process in a previous process, but I cannot find any difference that is causing the issue.
I have tried both with Hibernate 3.6.10 and 4.1.8 against MS SQL 2008 Express.
Any ideas? Thank in advance.
It doesn’t work because the AUTO strategy, for your database, consists in relying o the database to generate IDs. The ID column of the table should be an auto-increment column, or you should change the ID generation strategy.