Hibernate不会将数据保存到DB中吗?

the*_*ava 2 java spring hibernate

我有一个表单,在其中输入详细信息,但是当我单击“保存”时,它不会保存在数据库中……尽管表确实已创建。

我的联系方式

package your.intermedix.domain;

import java.io.Serializable;

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

@Entity
@Table(name="USER")

public class Contact implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;
    private String email;
    private String lastname;
    private String designation;

    @Id
    @GeneratedValue
    @Column(name="USER_ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="DESIGNATION")
    public String getDesignation(){
        return designation;
    }

    public void setDesignation(String designation){
        this.designation = designation;
    }

    @Column(name="EMAIL")
    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    @Column(name="LASTNAME")
    public String getLastname(){
        return lastname;
    }

    public void setLastname(String lastname){
        this.lastname= lastname;
    }

    @Column(name="FIRSTNAME")
    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }


    public String toString()
    {
        return "designation = '" + designation + "',email='"+ email +"', lastname='"+ lastname +"', name = '" + name + "'";
    }

}
Run Code Online (Sandbox Code Playgroud)

我的Application-Context.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Turn on AspectJ @Configurable support -->

<context:spring-configured />
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="your.intermedix"/>
<context:annotation-config/>
 <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

  <!-- a PlatformTransactionManager is still required -->
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- (this dependency is defined somewhere else) -->
  <property name="dataSource" ref="myDataSource"/>
  </bean>


<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />


    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>your.intermedix.domain.Contact</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
        <property name="username" value="monty"/>
        <property name="password" value="indian"/>
    </bean>   
</beans>
Run Code Online (Sandbox Code Playgroud)

我在控制台中没有收到任何错误。

更新的代码..

package your.intermedix.services;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import your.intermedix.domain.Contact;
import your.intermedix.services.IContact;

@Service
public class ContactSerImpl implements IContact {

    private HibernateTemplate hibernateTemplate;

        public void setSessionFactory(SessionFactory sessionFactory) {
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
            @Transactional
        public void saveContact(Contact contact) {
            System.out.println("Hello Guru contact");
            System.out.println(contact);
            hibernateTemplate.saveOrUpdate(contact);
        }

        public void hello() {
            System.out.println("Hello Guru");
        }
}
Run Code Online (Sandbox Code Playgroud)

我的打印对帐单的“我的服务”类

Boz*_*zho 5

您需要一个正在运行的事务。如果使用,Spring事务管理是必经之路HibernateTemplate。阅读文档。答案中包含的时间太长,但总之如下:

  • 您需要将事务管理器定义为spring bean
  • 你需要 <tx:annotation-driven />
  • 您需要使用以下注释您的事务处理方法 @Transactional