简体   繁体   English

使用 Struts 2 和 Hibernate 在数据库中插入空值

[英]Null values are insering in database using Struts 2 and Hibernate

I am using Struts 2 and Hibernate integration application to insert values in database.我正在使用 Struts 2 和 Hibernate 集成应用程序在数据库中插入值。 But after inserting values from a form filed only null value is saving in database.但是从提交的表单中插入值后,只有null值保存在数据库中。

This is my form JSP file:这是我的表单 JSP 文件:

employee.jsp : employee.jsp

<%@ taglib  uri ="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="employee" method="post">
<s:textfield name ="name" label="ENTER your name"/>
<s:textfield name="address" label="Enter Address"/>
<s:submit label="submit">submit</s:submit>

</s:form>
</body>
</html>

Entity class Empmodel.java :实体类Empmodel.java

package model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;


import javax.persistence.Id;
@Entity

public class Empmodel {
@Id @GeneratedValue
private int serialno;
private String name;
private String address;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}   
}

Connectionfactory.java : Connectionfactory.java :

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class ConnectionFactory {


private static  SessionFactory  sessionfactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
    
    
    Configuration config =null;
     SessionFactory sf  =null;
    try{
        config= new Configuration().configure();
        ServiceRegistry servicereg = (ServiceRegistry) new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
                
                sf= config.buildSessionFactory(servicereg); 
        
        
    }
    catch(Throwable e){
        System.out.println("Initial session factory creation failed"+ e);
        throw new ExceptionInInitializerError(e);
    }
    return sf;
}
 public static SessionFactory getSessionFactory(){
    return sessionfactory;
    }}

Empdao is: Empdao是:

import model.Empmodel;
import org.hibernate.Session;

public class Empdao {

public Empmodel add(){
    Empmodel model = new Empmodel();
    Session session=ConnectionFactory.getSessionFactory().getCurrentSession();
    
    session.beginTransaction();
    
    session.save(model);
    session.getTransaction().commit();
    return model;
}
}

Action class is: Action类是:

import model.Empmodel;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.Empdao;

public class Empaction extends ActionSupport implements ModelDriven<Empmodel> {
    private static final long serialVersionUID = 1L;
    private Empmodel model;
    
    
        public String execute() throws Exception{
            Empdao empdao = new Empdao();
            Empmodel queryresult =  empdao.add();
        
            if(queryresult != null) {
                return SUCCESS; 
            }
            else
                return ERROR;   
        }
        @Override
        public Empmodel getModel() {
            // TODO Auto-generated method stub
            return model;
        }
    }

if you want to get something first you need to put something.如果你想先得到一些东西,你需要放一些东西。 This rule works everywhere.这个规则适用于任何地方。 In your code you need to put the model object to DAO.在您的代码中,您需要将模型对象放入 DAO。 Then DAO will save it to the db using the values populated in the model via the interceptor.然后 DAO 将使用通过拦截器填充到模型中的值将其保存到数据库中。 For example例如

Empmodel queryresult = empdao.add(model); 

For this to work you need to change the method signature to add a parameter for model .为此,您需要更改方法签名以添加model参数。

The thing seems trivial but you need to remove the statement that recreate the model in the method implementation, it is not working fine.这件事似乎微不足道,但您需要删除在方法实现中重新创建model的语句,它无法正常工作。 Also make sure that the model is not empty before you start transaction.在开始交易之前,还要确保模型不为空。

The save method in DAO should check if the new object is created that has null value for id . DAO 中的save方法应该检查是否创建了idnull值的新对象。

if (model.getId() == null)
  session.save(model);
else
  session.update(model);
}

How to implement a ModelDriven with integration hibernate example 如何使用集成休眠示例实现ModelDriven

Struts2 hibernate integration via s2hibernate plugin 通过 s2hibernate 插件进行 Struts2 休眠集成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Hibernate将“ null”值插入数据库? - How to insert `null` values to database using Hibernate? 如何使用休眠从数据库获取空值和填充值 - How to get null and filled values from database using hibernate JDBC代码未插入数据库 - JDBC code not insering into Database 在Struts 2和Hibernate中使用分页 - Using a pagination with Struts 2 and Hibernate 如何在Hibernate Application中处理数据库空值? - How to handle database null values in Hibernate Application? 无法使用Hibernate和Struts从数据库中的JSP中显示图像 - Unable to show image in jsp from database using hibernate and struts 如何从数据库中检索实例并使用struts2和hibernate显示它? - How to retrieve an instance from the database and display it using struts2 and hibernate? 使用 hibernate 将一行插入数据库,我得到“1”,null 放入数据库而不是用户输入的值 - Inserting a row into a database using hibernate, I'm getting “1” and null put into the database instead of user inputted values 如何使用 Hibernate 仅更新数据库的一个属性...因为它由于 getter 和 setter 分配了 NULL 值? - How to update only one attribute of Database using Hibernate... Since it assigns NULL values due to getters and setters? Struts 2与Hibernate 3集成空指针异常 - struts 2 integration with hibernate 3 null pointer exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM