我有如下的JPA实体类(示例),一个房子只属于一条街道,而一条街道可以有多个房子。
@Entity
public class House {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer id;
public String name
@ManyToOne
public Street street;
}
@Entity
public class Street {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer id;
@OneToMany(mappedBy="street")
public Set<House> houses;
}
我将生成类型设置为identity,这应该会自动分配一个新的ID。
在创建一个新的House和一个新的Street时,我必须先创建并持久化Street,然后再创建House。这是因为我没有设置CascadeType为PERSIST,所以必须手动完成[1]。但是,在插入一个新创建的Street时:
Street street = new Street();
entityManager.persist(street);
Hibernate/JPA 生成了以下 SQL 查询:
insert into Street default values
这是MySQL不喜欢的。
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default values' at line 1
有什么想法吗?我正在使用Java 6,MySQL 5.0.67以及Hibernate实现的JPA(版本3.2.1.ga)。
[1] EJB 3 in Action第318-319页。