我正在使用Hibernate Relationship来开发Spring和Hibernate Web应用程序,在前三次工作中,它能够成功地从数据库中获取记录且没有问题,但在第四次尝试时,应用程序会卡住或冻结。当我重新启动Tomcat服务器后,一切都正常,但再次尝试第四次操作后会再次卡住或冻结。应用程序卡住或冻结时没有任何错误提示,这让我很困惑。当我测试另一个未使用Hibernate关系的类时,一切都正常。我怀疑是Hibernate关系引起的问题,但我不确定。
以下是我的Hibernate关系参考: mkyong viralpatel 这是我的ERD。
任何帮助都将是乐趣 :D ~
以下是我的Hibernate关系参考: mkyong viralpatel 这是我的ERD。
这是我的用户类,
@SuppressWarnings("serial")
@Entity
@Table(name="user")
public class User implements Serializable{
private String no_dana;
private String npp;
@Id
@Column(name="no_dana", unique=true, nullable=false, updatable=false)
public String getNo_dana() {
return no_dana;
}
public void setNo_dana(String no_dana) {
this.no_dana = no_dana;
}
@Column(name="npp")
public String getNpp() {
return npp;
}
private Set<Tanya> tanya;
@OneToMany(fetch= FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user")
public Set<Tanya> getTanya() {
return tanya;
}
public void setTanya(Set<Tanya> tanya) {
this.tanya = tanya;
}
private Set<Jawab> jawab;
@OneToMany(fetch = FetchType.LAZY, mappedBy="user")
public Set<Jawab> getJawab() {
return jawab;
}
public void setJawab(Set<Jawab> jawab) {
this.jawab = jawab;
}
}
这是我的 Tanya 类,
@SuppressWarnings("serial")
@Entity
@Table(name="tanya")
public class Tanya implements Serializable{
private int id_tanya;
private String isi;
@Id
@Column(name="id_tanya", unique=true, nullable=false)
public int getId_tanya() {
return id_tanya;
}
public void setId_tanya(int id_tanya) {
this.id_tanya = id_tanya;
}
@Column(name="isi")
public String getIsi() {
return isi;
}
public void setIsi(String isi) {
this.isi = isi;
}
private User user;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="no_dana")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
private Set<Jawab> jawab;
@OneToMany(fetch = FetchType.LAZY, mappedBy="tanya")
public Set<Jawab> getJawab() {
return jawab;
}
public void setJawab(Set<Jawab> jawab) {
this.jawab = jawab;
}
}
这是我的Jawab类。
@SuppressWarnings("serial")
@Entity
@Table(name="jawab")
public class Jawab implements Serializable{
private int id_jawab;
private String isi;
@Id
@Column(name="id_jawab", unique=true, nullable=false)
public int getId_jawab() {
return id_jawab;
}
public void setId_jawab(int id_jawab) {
this.id_jawab = id_jawab;
}
@Column(name="isi")
public String getIsi() {
return isi;
}
public void setIsi(String isi) {
this.isi = isi;
}
private User user;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="no_dana")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
private Tanya tanya;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id_tanya")
public Tanya getTanya() {
return tanya;
}
public void setTanya(Tanya tanya) {
this.tanya = tanya;
}
}
这是我的dao示例(来自Tanya类),
@Autowired
private TanyaDaoImpl(SessionFactory sessionFactory){
setSessionFactory(sessionFactory);
}
@SuppressWarnings("unchecked")
@Override
public Tanya get(int id_tanya)throws Exception{
DetachedCriteria criteria = DetachedCriteria.forClass(Tanya.class).add(Restrictions.eq("id_tanya", id_tanya));
List<Tanya> tanyaList = getHibernateTemplate().findByCriteria(criteria);
return tanyaList.get(0);
}
@Override
public Tanya getTanya(int id_tanya)throws Exception{
Query query = getSession().createQuery("from Tanya where id_tanya = :id_tanya");
query.setParameter("id_tanya", id_tanya);
return (Tanya) query.list().get(0);
}
@Override
public void save(Tanya tanya)throws Exception{
getHibernateTemplate().save(tanya);
}
@Override
public void update(Tanya tanya)throws Exception{
getHibernateTemplate().update(tanya);
}
@Override
public void delete(Tanya tanya)throws Exception{
getHibernateTemplate().delete(tanya);
}
@SuppressWarnings("unchecked")
@Override
public List<Tanya> listAllTanya()throws Exception{
DetachedCriteria criteria = DetachedCriteria.forClass(Tanya.class);
List<Tanya> tanyaList = getHibernateTemplate().findByCriteria(criteria);
return tanyaList;
}
这是我的会话配置,
<context:component-scan base-package="org.ppbni.splatter" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_ppbni" />
<property name="username" value="root" />
<property name="password" value="shikamaru" />
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ppbniorg_db" />
<property name="username" value="ppbniorg_user" />
<property name="password" value="shikamaru" /> -->
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="org.ppbni.splatter.model" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
任何帮助都将是乐趣 :D ~