当使用JPA配置实体时,如果有两个属性(一个是一般属性,一个是多对一的属性)映射到数据库的同一列,就会报错。
这时,在多对一的@JoinColumn注解中添加insertable = false, updatable = false就能解决。
例如:
-
-
-
- @Entity
- @Table(name = "TBL_SYS_USER")
- public class SysUser{
- @Id
- @GeneratedValue(generator = "system-uuid")
- @GenericGenerator(name = "system-uuid", strategy = "uuid")
- @Column(name = "USER_ID", unique = true, length = 40)
- @CsvColumn(imported=false)
- private String id;
-
- @Column(name = "USER_CODE", nullable = false, length = 40)
- private String userCode;
-
- @CsvColumn(desc="用户姓名", required= true )
- @Column(name = "USER_NAME", length = 60)
- private String userName;
-
- @Column(name = "DEPT_ID", length = 40)
- private String deptId;
-
- @ManyToOne
- @JoinColumn(name = "DEPT_ID", insertable = false, updatable = false, nullable=true)
- private SysOrg dept;
-
- }
我们可以看到,deptId和dept都映射到数据库里面的DEPT_ID字段,但是加上了insertable = false, updatable = false后,在保存数据时,dept不会被保存到数据库。