2017-04-24 134 views
0

我目前正試圖將spring與spring集成。我使用dao設計模式和mysql作爲數據庫。我試圖在數據庫中添加contacte實體,但沒有添加,也沒有顯示錯誤。順便說一下,我可以獲取聯繫人列表並通過ID獲取它,但我無法更新或插入。休眠和彈簧綜合問題

這是我的背景文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     "> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/mailmaneger" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
     <property name="defaultAutoCommit" value="false" /> 
    </bean> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="mysessionFactory" /> 

    </bean> 
    <bean id="mysessionFactory" 
     class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
      <property name="packagesToScan" value="biz.picosoft.entity"/> 


     <property name="dataSource" ref="dataSource"></property> 


     <property name="hibernateProperties"> 

      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <prop key="hibernate.show_sql">true</prop> 


      </props> 
     </property> 
    </bean> 

    <bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate"> 
     <property name="sessionFactory" ref="mysessionFactory"></property> 
     <property name="checkWriteOperations" value="false"></property> 
    </bean> 

    <bean id="d" class="biz.picosoft.daoImpl.ContacteDaoImpl"> 
     <property name="template" ref="template"></property> 
    </bean> 



</beans> 

通用的DAO文件

package biz.picosoft.daoImpl; 

import java.lang.reflect.ParameterizedType; 
import java.lang.reflect.Type; 
import java.util.List; 

import org.springframework.orm.hibernate5.HibernateTemplate; 
import org.springframework.transaction.annotation.Transactional; 

import dao.GenericDao; 

@Transactional(readOnly = false) 
public class GenericDaoImp<T> implements GenericDao<T> { 
    HibernateTemplate template; 

    protected Class<T> daoType; 

    public GenericDaoImp() { 
     Type t = getClass().getGenericSuperclass(); 
     ParameterizedType pt = (ParameterizedType) t; 
     daoType = (Class) pt.getActualTypeArguments()[0]; 
    } 

    public HibernateTemplate getTemplate() { 
     return template; 
    } 

    public void setTemplate(HibernateTemplate template) { 
     this.template = template; 
    } 

    public void insert(T t) { 
     // TODO Auto-generated method stub 
     template.save(t); 

    } 

    public void update(T t) { 
     // TODO Auto-generated method stub 
     template.update(t); 
    } 

    public void delete(T t) { 
     // TODO Auto-generated method stub 
     template.delete(t); 
    } 

    public T findById(Class<T> t, String id) { 
     // TODO Auto-generated method stub 
     return template.get(t, id); 
    } 

    public List<T> findAll() { 
     // TODO Auto-generated method stub 
     return template.loadAll(daoType); 
    } 

} 

contacte DAO文件

package dao; 

import biz.picosoft.entity.Contacte; 

public interface ContacteDao extends GenericDao<Contacte> { 

} 

contactedaoimpl

package biz.picosoft.daoImpl; 

import org.springframework.transaction.annotation.Transactional; 

import biz.picosoft.entity.Contacte; 
import dao.ContacteDao; 

@Transactional(readOnly = false) 
public class ContacteDaoImpl extends GenericDaoImp<Contacte> implements ContacteDao { 

} 

實體

package biz.picosoft.entity; 

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name = "Contacte") 
public class Contacte implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "idContact") 
    int idContact; 
    @Column(name = "nom") 
    String nom; 
    @Column(name = "mail") 
    String mail; 
    @Column(name = "téléphone") 
    String téléphone; 
    @Column(name = "adresse") 
    String adresse; 

    public Contacte() { 
     super(); 
    } 

    public Contacte(String nom, String mail, String téléphone, String adresse) { 
     super(); 

     this.nom = nom; 
     this.mail = mail; 
     this.téléphone = téléphone; 
     this.adresse = adresse; 
    } 

    public long getIdContact() { 
     return idContact; 
    } 

    public void setIdContact(int idContact) { 
     this.idContact = idContact; 
    } 

    public String getNom() { 
     return nom; 
    } 

    public void setNom(String nom) { 
     this.nom = nom; 
    } 

    public String getMail() { 
     return mail; 
    } 

    public void setMail(String mail) { 
     this.mail = mail; 
    } 

    public String getTéléphone() { 
     return téléphone; 
    } 

    public void setTéléphone(String téléphone) { 
     this.téléphone = téléphone; 
    } 

    public String getAdresse() { 
     return adresse; 
    } 

    public void setAdresse(String adresse) { 
     this.adresse = adresse; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + (int) (idContact^(idContact >>> 32)); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Contacte other = (Contacte) obj; 
     if (idContact != other.idContact) 
      return false; 
     return true; 
    } 

} 

我的主要

package biz.picosoft.mains; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; 

import biz.picosoft.daoImpl.ContacteDaoImpl; 
import biz.picosoft.entity.Contacte; 

public class TestHibernate { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Contacte contacte = new Contacte("fatma", "test2", "test", "test"); 

     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
     contacte.setIdContact(4); 
     ContacteDaoImpl contacteDaoImpl = (ContacteDaoImpl) context.getBean("d"); 
     contacteDaoImpl.insert(contacte); 

    } 

} 
+0

你得到什麼錯誤訊息? –

+0

我沒有錯誤信息 –

+0

你已經上傳了代碼到github,以便我們可以在本地嘗試它? –

回答

1

你並不需要在所有的DAO實現添加@Transactional註釋,如果你已經添加@Transactional註釋上GenericDaoImp,和其他東西放在@Transactional(readOnly = false)註釋on方法你只能獲取數據。這是唯一的建議,僅在

<property name="defaultAutoCommit" value="true" />

更換<property name="defaultAutoCommit" value="false" />
我已經試過了,其工作對我來說,你的問題將得到解決。

+0

我做到了,但它不工作 –

+0

讓我得到拉爾代碼.. –

+0

是的,它工作後,我用 =「true」/>。 謝謝:) –

0

問題可能是您需要啓用註釋支持以及事務。嘗試添加以下到您的Spring XML:

<context:annotation-config/> 
<tx:annotation-driven /> 

僅供參考,你也應該看看:Why HibernateTemplate isn't recommended?