hibernate 多对一映射问题
举个栗子
@Entity
@Table(name="alipay_account")
public class AlipayAccount {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Long id;
// 创建时间
@Column(name="create_time" ,insertable =false)
private Date createTime;
// 支付宝帐号
@Column(name="account", length=50 )
private String account;
set/get省略 ....
}
public class User{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private long id;
//用户名
@Column(name="username", length=50 )
private String username;
@ManyToOne(fetch =FetchType.LAZY)
@JoinColumn(name="alipay_id", referencedColumnName="id" )
private AlipayAccount alipayAccount;
set/get省略 ....
}
遇到的问题:1.一个用户可能没有支付宝帐号,因此在表中存的字段alipay_id 为0,这就导致了hibernate查询时会抛出
No row with the given identifier exists: [cn.edu.dragon.model.admin.AlipayAccount#0]的异常
2.为解决1的问题我通过查资料使用注解@NotFound(action=NotFoundAction.IGNORE),这又会引起新的问题,将懒加载
FetchType.LAZY属性覆盖了,变急加载了,当查list的时候极大影响的查询效率
在线坐等大神解决,谢谢啦,困扰我两天了