简体   繁体   中英

Spring 4 and hibernate 5 integration

I am currently trying to integrate hibernate with spring.I am using the dao design patter and mysql as database. i am trying to add the contacte entity in the db but i got this error

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
    at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1165)
    at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:643)
    at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:640)
    at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:359)
    at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:326)
    at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:640)
    at biz.picosoft.daoImpl.ContacteDaoImpl.inserteContacte(ContacteDaoImpl.java:20)
    at biz.picosoft.mains.TestHibernate.main(TestHibernate.java:21)

this is my context file

<?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="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>
    </bean>

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

</beans>

my main file

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", "test",  "test",  "test");
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");

ContacteDaoImpl contacteDaoImpl=(ContacteDaoImpl) context.getBean("d");


 contacteDaoImpl.inserteContacte(contacte) ;
    }

}

my dao file

package biz.picosoft.daoImpl;

import java.util.List;

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

import biz.picosoft.entity.Contacte;

public class ContacteDaoImpl implements ContacteDao{
    HibernateTemplate template;  
    public void setTemplate(HibernateTemplate template) {  
        this.template = template;  
    } 


    public void inserteContacte(Contacte contacte) {
        // TODO Auto-generated method stub
        template.save(contacte);

    }

    public void updateContacte(Contacte contacte) {
        // TODO Auto-generated method stub
        template.update( contacte );
    }

    public void deleteContacte(Contacte contacte) {
        // TODO Auto-generated method stub
        template.delete(contacte);
    }

    public Contacte getContacteById(int id) {
        // TODO Auto-generated method stub

        return template.get(Contacte.class, id);
    }

    public List<Contacte> getAll() {
        // TODO Auto-generated method stub
        return template.loadAll(Contacte.class);
    }
    public HibernateTemplate getTemplate() {
        return template;
    }

}

my Entity file

package biz.picosoft.entity;

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 {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 @Column(name = "idContact")
long 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(  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 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;
}


}

my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd
          http://www.springframework.org/schema/context/spring-context.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.picosoft.gestionCourrier</groupId>
    <artifactId>gestion-courrier</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>maven.alfresco.com</id>
            <name>Alfresco Maven Repository</name>
            <url>http://maven.alfresco.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring</artifactId>
            <version>5.14</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-engine</artifactId>
            <version>5.14</version>
        </dependency>
        <dependency>
            <groupId>org.alfresco.cmis.client</groupId>
            <artifactId>alfresco-opencmis-extension</artifactId>
            <version>0.9</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.193</version>
        </dependency>
        <dependency>
            <groupId>org.alfresco.cmis.client</groupId>
            <artifactId>alfresco-opencmis-extension</artifactId>
            <version>0.2</version>
            <type>jar</type>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
    </dependencies>
</project>

Most probably you missed to trigger transaction. In your context.xml add <tx:annotation-driven/> for declarative transaction support. And add @Transactional(readOnly=false) over the ContacteDaoImpl methods.

This should solve your problem. If not, there might be something else.

i fixed this problem by changing those beans,but it seems it is like creating the tables but values are not added in the table

<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="mysessionFactory"></property>
    <property name="checkWriteOperations" value="false"></property>
</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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM