Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 冬眠不';MySQL数据库中的t更新记录_Java_Mysql_Spring_Hibernate_Generics - Fatal编程技术网

Java 冬眠不';MySQL数据库中的t更新记录

Java 冬眠不';MySQL数据库中的t更新记录,java,mysql,spring,hibernate,generics,Java,Mysql,Spring,Hibernate,Generics,我正在应用程序中使用PrimeFaces 3.5,JSF 2.2,Hibernate 4.1,Spring 3.2.3,MySQL。 函数updateUser()应该从PrimeFaces dataTable组件中为所选用户更新数据库中的记录(值是正确的),但由于未知原因,它没有更新。我有一个名为AbstractDAO的类,它通过泛型实现CRUD操作。插入、选择和删除都可以完美地工作,但更新失败,不会显示任何错误。我的代码有什么错误吗 applicationContext.xml: &

我正在应用程序中使用
PrimeFaces 3.5
JSF 2.2
Hibernate 4.1
Spring 3.2.3
MySQL
。 函数updateUser()应该从PrimeFaces dataTable组件中为所选用户更新数据库中的记录(值是正确的),但由于未知原因,它没有更新。我有一个名为AbstractDAO的类,它通过泛型实现CRUD操作。插入、选择和删除都可以完美地工作,但更新失败,不会显示任何错误。我的代码有什么错误吗


applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">


    <!--            GLOABL SETTINGS             -->


    <context:component-scan base-package="com.infostroy.adminportal"/>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>


    <!--        DATA SOURCE AND PERSISTENCE SETTINGS       -->


    <bean id="propertiesPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dmDataSource"/>
        <property name="packagesToScan" value="com.infostroy.adminportal"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${db.dialect}</prop>
                <prop key="hibernate.show_sql">${db.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${db.hbm2ddl_auto}</prop>
                <prop key="connection.pool_size">${db.pool_size}</prop>
                <prop key="current_session_context_class">${db.current_session_context_class}</prop>
                <prop key="org.hibernate.FlushMode">${db.flush_mode}</prop>
            </props>
        </property>
    </bean>


    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="dataSource" ref="dmDataSource" />
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <bean id="dmDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="maxWait" value="5000" />
        <property name="initialSize" value="2" />
        <property name="maxActive" value="100"/>
        <property name="maxIdle" value="50"/>
        <property name="minIdle" value="0"/>
    </bean>

</beans>
抽象道:

public abstract class AbstractDAO<T extends Serializable> implements Serializable {

@Autowired
protected SessionFactory sessionFactory;
protected T object;
protected Class clazz;

public AbstractDAO(Class clazz) {
    this.clazz = clazz;
}

//Executes before being removed from container
@PreDestroy
protected void destroy() {
    sessionFactory.getCurrentSession().close();
}

public Session getHiberSession() {
    return sessionFactory.openSession();
}

@Transactional
protected T getByID(int id) {
    String queryString = "from " + clazz.getSimpleName() + " where id = :id";
    Query query = getHiberSession().createQuery(queryString);
    query.setInteger("id", id);
    object = (T) query.uniqueResult();
    return object;
}

@Transactional
protected int deleteByID(int id) {
    String queryString = "delete " + clazz.getSimpleName() + " where id = :id";
    Query query = getHiberSession().createQuery(queryString);
    query.setInteger("id", id);
    return query.executeUpdate();
}

@Transactional
protected boolean insert(T object) {
    try {
        getHiberSession().save(object);
        return true;
    } catch (HibernateException ex) {
        return false;
    }
}

@Transactional
protected boolean update(T object) {
    try {
        getHiberSession().saveOrUpdate(object);
        return true;
    } catch (HibernateException ex) {
        return false;
    }
}

@Transactional
protected List getAllRecords() {
    String queryString = "from " + clazz.getSimpleName();
    Query query = getHiberSession().createQuery(queryString);
    return query.list();
}
公共抽象类AbstractDAO实现可序列化{
@自动连线
受保护的SessionFactory SessionFactory;
受保护对象;
保护类clazz;
公共抽象道(类clazz){
this.clazz=clazz;
}
//在从容器中删除之前执行
@发情前期
受保护的无效销毁(){
sessionFactory.getCurrentSession().close();
}
公开会话getHiberSession(){
返回sessionFactory.openSession();
}
@交易的
受保护的T getByID(int id){
字符串queryString=“from”+clazz.getSimpleName()+“其中id=:id”;
Query Query=getHiberSession().createQuery(queryString);
query.setInteger(“id”,id);
object=(T)query.uniqueResult();
返回对象;
}
@交易的
受保护的int-deleteByID(int-id){
字符串queryString=“delete”+clazz.getSimpleName()+“其中id=:id”;
Query Query=getHiberSession().createQuery(queryString);
query.setInteger(“id”,id);
返回query.executeUpdate();
}
@交易的
受保护的布尔插入(T对象){
试一试{
getHiberSession().save(对象);
返回true;
}捕获(HibernateeException例外){
返回false;
}
}
@交易的
受保护的布尔更新(T对象){
试一试{
getHiberSession().saveOrUpdate(对象);
返回true;
}捕获(HibernateeException例外){
返回false;
}
}
@交易的
受保护列表getAllRecords(){
字符串queryString=“from”+clazz.getSimpleName();
Query Query=getHiberSession().createQuery(queryString);
返回query.list();
}
}

UserDAO.java:

@Repository
public class UserDAO extends AbstractDAO<User> {

public UserDAO() {
    super(User.class);
}

public User getUserById(int id) {
    return super.getByID(id);
}

public int deleteUserById(int id) {
    return super.deleteByID(id);
}

public boolean insertUser(User user) {
    return super.insert(user);
}

public boolean updateUser(User user) {
    return super.update(user);
}

public List<User> getAllUsers() {
    return super.getAllRecords();
}
}
@存储库
公共类UserDAO扩展了AbstractDAO{
公共用户dao(){
super(User.class);
}
公共用户getUserById(int id){
返回super.getByID(id);
}
公共int-deleteUserById(int-id){
返回super.deleteByID(id);
}
公共布尔插入器(用户){
返回super.insert(用户);
}
公共布尔更新器(用户){
返回super.update(用户);
}
公共列表getAllUsers(){
返回super.getAllRecords();
}
}

如果你需要任何额外的信息或代码-只要告诉我。每一个答案都受到高度赞赏,并立即得到回应


谢谢。

我刚开始使用Hibernate,但我使用的是sessionFactory.getCurrentSession()而不是openSession()

我刚开始使用Hibernate,但我使用的是sessionFactory.getCurrentSession()而不是openSession()

在saveOrUpdate()方法之后,只需刷新会话即可。例如:session.flush()

有关更多详细信息,请参见我的博客:

保存或更新()后,我只需刷新会话即可。例如:session.flush()

有关更多详细信息,请参见我的博客:


我刚开始使用Hibernate,但我使用的是sessionFactory.getCurrentSession()而不是openSession().OMG,用getCurrentSession代替openSession!:D另一个问题是为什么它对其他功能有效?请将您的响应作为答案发布,以便我可以接受。当您使用Spring(@TRansactional)管理的事务时,框架会在th方法之前获得一个会话并启动一个事务,然后提交它。当您获取CurrentSession时,您使用相同的会话(和事务),这是正常的,但是openSession()获取另一个未提交的会话。只是一个猜测。现在有道理了。太好了。很高兴我能帮忙!!我刚开始使用Hibernate,但我使用的是sessionFactory.getCurrentSession()而不是openSession().OMG,用getCurrentSession替换openSessionD另一个问题是为什么它对其他功能有效?请将您的响应作为答案发布,以便我可以接受。当您使用Spring(@TRansactional)管理的事务时,框架会在th方法之前获得一个会话并启动一个事务,然后提交它。当您获取CurrentSession时,您使用相同的会话(和事务),这是正常的,但是openSession()获取另一个未提交的会话。只是一个猜测。现在有道理了。太好了。很高兴我能帮忙!!会话在提交期间被隐式刷新,并由Spring以声明方式管理,因此管理会话、事务和刷新没有任何意义。即使在saveOrUpdate语句之后显式使函数调用刷新,也无法解决问题。解决方案是调用现有会话,而不是创建新会话。不过,感谢您的贡献。会话在提交期间被隐式刷新,并由Spring以声明方式管理,因此管理会话、事务和刷新没有任何意义。即使在saveOrUpdate语句之后显式使函数调用刷新,也无法解决问题。解决方案是调用现有会话,而不是创建新会话。不过,谢谢你的贡献。
@Repository
public class UserDAO extends AbstractDAO<User> {

public UserDAO() {
    super(User.class);
}

public User getUserById(int id) {
    return super.getByID(id);
}

public int deleteUserById(int id) {
    return super.deleteByID(id);
}

public boolean insertUser(User user) {
    return super.insert(user);
}

public boolean updateUser(User user) {
    return super.update(user);
}

public List<User> getAllUsers() {
    return super.getAllRecords();
}
}