我有一个@GenericGenerator
的实体:
@Entity
public class Report {
@Id
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "examrequisitions"))
// @GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false, unique = true)
private Long id;
... // outcome, status
}
当我保存该实体时,它会显示以下异常:
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null
Error Code: 1048
Call: INSERT INTO SystemCardiologyReports.REPORTS (ID, OUTCOME, STATUS) VALUES (?, ?, ?)
bind => [3 parameters bound]
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'id' cannot be null
为什么ID null
- 尽管@GenericGenerator
?
答案 0 :(得分:1)
GenericGenerator
注释只是在代码中的某处声明了一个生成器。您可以将其添加到任何包,类型,字段或方法。这并不意味着生成器应该用于带注释的元素。
您需要将@GeneratedValue
添加到要使用它的字段,并按名称引用它:
@Id
@GenericGenerator(name = "genericGenerator" ...))
@GeneratedValue(generator = "genericGenerator")
private Long id;