我正在使用带有Hibernate 4.x的JPA 2.1。我发现@MakKeyColumn不是在打瞌睡。
@Entity
public class Department {
@Id private int id;
@OneToMany(mappedBy = "department")
@MapKeyColumn(name = "cub_id")
private Map<String, Employee> employeesByCubicle = new HashMap<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Map<String, Employee> getEmployeesByCubicle() {
return employeesByCubicle;
}
public void setEmployeesByCubicle(Map<String, Employee> employeesByCubicle) {
this.employeesByCubicle = employeesByCubicle;
}
}
如果执行上面的代码,我可以看到以下错误
Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "cub_id" violates not-null constraint
Detail: Failing row contains (2, xxxxxx, null, null).
但是,如果我省略@MapKeyColumn
,它就可以了。任何人都可以告诉我为什么在使用@MapKeyColumn
感谢您提前回复:)
答案 0 :(得分:0)
尝试将MapKeyColumn定义为可为空:
from django.conf.urls import include, url
from .app_views import index
urlpatterns = [
url(r'^.*$', index, name='app'),
]
可以为null的属性的默认值为&#39; false&#39;。