2011-01-03 6 views
2

나는 세부 사항을 입력하는 양식이 있지만 저장을 클릭하면 데이터베이스에 저장되지 않는다 ... 테이블이 생성 되더라도.Hibernate는 DB에 데이터를 저장하지 않습니까?

내 연락 POJO

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 + "'"; 
    } 

} 

내 응용 프로그램의 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> 

.

업데이트 코드 .. 내가 인쇄 문이 나는 봄 - 하이버 네이트 프레임 워크를 잘 모르겠지만, 데이터가 기록되지 않을 때 종종 그것이 menas

+0

세이브 코드를 제공해 주시겠습니까? 새 개체를 만들고 저장하는 방법. – Kiva

+0

엔티티 및 구성을 표시했지만 관련 부분을 표시하지 않았습니다. 즉, 양식을 처리하는 코드입니다. – darioo

+0

코드가 업데이트되었습니다. – theJava

답변

2

작동

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"); 
     } 
} 

내 서비스 클래스 아니 databackend에 flushed. 당신에게주는 것은

System.err.println(hibernateTemplate.getFlushMode()); 
+0

이 문은 자체적으로 호출되지 않습니다. – theJava

4

실행 트랜잭션이 필요합니다. HibernateTemplate을 사용하는 경우 Spring transaction management을 사용할 수 있습니다. 설명서를 읽으십시오. 이 답변에 포함하는 데 시간이 너무 오래지만, 여기에 짧은에 : 당신은 당신이 <tx:annotation-driven />

  • 당신이 @Transactional과의 거래 방법에 주석을해야 할 필요가
  • 스프링 빈으로 트랜잭션 관리자를 정의 할 필요가

  • +0

    트랜잭션 관리자 및 기타 자료를 추가 한 주요 게시물을 업데이트했습니다. 그래도 객체는 DB에서 지속되지 않습니다. – theJava

    +0

    @theJava - 당신은 <속성 이름 = "sessionFactory에"REF = "mySessionFactory"/> I HibernateTransactionManager를 – Bozho

    +0

    <= "org.springframework.orm.hibernate3.HibernateTransactionManager"콩 ID = "myTxManager"클래스>를 필요 했어 이걸로 업데이 트 와도 작동하지 않습니다 – theJava

    관련 문제