现在有两张表一个用户,一个组织机构用户表有个外键关联组织机构现在添加一个用户,这个外键为空就出错的
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:用注解怎么配置能设置这个外键为空添加进去的
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:用注解怎么配置能设置这个外键为空添加进去的
public ProductType getProductType() {
return productType;
}
以上面的为列:optional属性表示ProductType是否可以为空,true表示可以为null,false表示不能为空
默认就是true,换句话说就是指产品表中的类型外键可以为null,按楼主的就是用户表中的对应的组织外键可以为null
关联的组织实体必须为persistent状态也就是处于持久状态才可以,
可以理解用户对应的这个组织实体数据库有对应的数据
这样设置也是出现错误
@ManyToOne(optional=true)
出现这个
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing:
@JoinColumn(name = "ENTERPRISE")
private Enterprise enterprise;这样子的,如果有加入这个外键就可以添加的,如果为空就不能添加的
你查看下表结构看外键是不是可以设置为null
意思就是你这样的啦,设置为true,但是不知道为什么不行的
@Column(name = "VENDER_CODE")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id; @Column(name = "MOBILE_NO")
private String mobileNo; @ManyToOne(optional=true)
@JoinColumn(name = "ENTERPRISE")
private Enterprise enterprise;
就这么配置的,但是就是不能添加的
@Column(name = "VENDER_CODE")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id; @Column(name = "MOBILE_NO")
private String mobileNo; @ManyToOne(optional=true)
@JoinColumn(name = "ENTERPRISE")
private Enterprise enterprise;
就这么配置的,但是就是不能添加的