在我的hibernate 5.0
中,我正在使用EntityManager.find(class,id)
,并且它正在基于ID提取对象,但是不一致,有时它不能提取数据,并且DB (MySql)
中的数据可用,我也尝试了EntityMa,nager.CreateQuery("")
,但还是遇到了同样的问题,因为调试并发现它没有击中数据库,所以我无法找出根本原因。以下是我的DAO and model
@Override
public User getUserById(int userId)
{
User user = null;
try {
logger.info("fetching user details of userid :"+userId);
user = entityManager.find(User.class,userId);
} catch (HibernateException e) {
logger.error("Error finding user : " + e);
}
return user;
}
型号:
@Entity @Table(name = "user",
uniqueConstraints= @UniqueConstraint(columnNames={"email_id"}))
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name = "user_id")
private int userId;
/** some more column and getter setter **/
//Mappings
@JsonIgnore
@Access(AccessType.PROPERTY)
@OneToMany(mappedBy = "userId", fetch = FetchType.EAGER)
private Set<Contract_Reviewer> contract_Reviewer;
@ManyToOne(fetch = FetchType.EAGER,cascade = {CascadeType.ALL}, optional = false)
@JoinColumn(name = "department_id")
private Department departments;
}