gpt4 book ai didi

java - Hibernate不将数据保存到DB中?

转载 作者:行者123 更新时间:2023-12-02 14:11:26 25 4
gpt4 key购买 nike

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

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

}

我的应用程序上下文.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>

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

更新了代码..

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

我的服务类,其中打印语句工作

最佳答案

您需要一个正在运行的事务。 Spring transaction management如果使用 HibernateTemplate 就是要走的路。阅读文档。答案太长,无法包含在内,但简而言之:

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

关于java - Hibernate不将数据保存到DB中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4585080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com