Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA2:如何使用@MapKeyColumn持久化数据_Java_Hibernate_Jpa_Annotations_Jpa 2.0 - Fatal编程技术网

Java JPA2:如何使用@MapKeyColumn持久化数据

Java JPA2:如何使用@MapKeyColumn持久化数据,java,hibernate,jpa,annotations,jpa-2.0,Java,Hibernate,Jpa,Annotations,Jpa 2.0,在我的示例中,我有两个实体要持久化。但是当我尝试使用@MapKeyColumn注释时,我对如何使用该注释以及如何持久化数据感到困惑。这是“PRO JPA2手册”中解释的示例。当我尝试运行此代码时,它将生成异常 两个实体: @Entity @Access(AccessType.FIELD) public class Employee { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String

在我的示例中,我有两个实体要持久化。但是当我尝试使用@MapKeyColumn注释时,我对如何使用该注释以及如何持久化数据感到困惑。这是“PRO JPA2手册”中解释的示例。当我尝试运行此代码时,它将生成异常

两个实体:

@Entity
@Access(AccessType.FIELD)
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name; 
private String salary;

@Enumerated(EnumType.STRING)
private EmployeeType type;

@ManyToOne
@JoinColumn(name="dept_id")
private Department deparment;

我的主要代码:

tr.begin();
        Department dept = deps.createDepartment("Sci");
        Employee emp = es.createEmployee("Harmeet Singh", "500" , "5684", dept);
        es.setEmpCabin(emp, dept, "10");
tr.commit();
当我尝试使用setempcoard(…)函数插入数据时,它将生成异常

Caused by: org.hibernate.exception.GenericJDBCException: Field 'cabin_id' doesn't have a default value

我怀疑您已将cabin_id列定义为
not null
,没有默认值。您的代码创建了一个没有CAB_id的员工,因此数据库不知道在该字段中存储什么。因此,waht是存储数据的正确方法。请用代码解释。我对映射中的映射没有太多经验。但首先要删除NOTNULL约束,看看它是否会产生影响。您还可以将CAB_id列映射为Employee中的字段,并使用MapKey注释。当我从Employee表中删除not null时,CAB_id的值为null。如果关联是双向的(因此由另一方映射),我不确定您是否可以使用此注释。尝试映射CAB_id列,并使用MapKey注释。
tr.begin();
        Department dept = deps.createDepartment("Sci");
        Employee emp = es.createEmployee("Harmeet Singh", "500" , "5684", dept);
        es.setEmpCabin(emp, dept, "10");
tr.commit();
Caused by: org.hibernate.exception.GenericJDBCException: Field 'cabin_id' doesn't have a default value