首页 > 解决方案 > 更新方法struts 2-hibernate-jpa

问题描述

struts.xml 这是 Struts 类


请告诉我使用strut2 hibernate JPA 更新的正确方法关于按 id 更新

<struts>
<package name="com.Attendance" extends="struts-default">
    <action name="RegTeacher" class="com.Attendance.TeacherAction" >
    <result name="success">/Admin/success.jsp</result>
    <result name="error">/Admin/RegistrationTeacher.jsp</result>
    <result name="input">/Admin/RegistrationTeacher.jsp</result>
    </action>

    <action name="EditTeacher" class="com.Attendance.TeacherAction" 
    method="FindById">
    <result name="success">/Admin/UDTeacher.jsp</result>
    <result name="input">/Admin/ShowAllTeacher.jsp</result>
    </action>

    <action name="UDTeacher" class="com.Attendance.TeacherAction" 
    method="Update">
    <result name="success">/Admin/success.jsp</result>
    <result name="error">/Admin/UDTeacher.jsp</result>
    <result name="input">/Admin/UDTeacher.jsp</result>
    </action>
   </package>
   <constant name="struts.action.excludePattern" 
   value="/CheckCode,/CheckRegistrationAdmin," />
  </struts>`

RegTeacher 这是模型/实体类

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新

  ` package com.Attendance;

   import javax.persistence.*;


   @Entity
   @Table(name="Teacher")
   public class RegTeacher{
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "id", nullable = false)
   private int id;
   @Column(name = "LecturerName",unique=true,nullable=false)
   private String Name;
   @Column(name= "email",unique=true,nullable=false)
   private String email;
   @Column(name= "pass",nullable=false)
   private String Password;
   @Column(name="Course",nullable=false)
   private String CourseT;
   @Column(name= "ClassName",nullable=false)
   private String CName;
 /** setter and getter **/
  }`

TecaherAction 这是 Action 类

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新

  `public class TeacherAction extends ActionSupport implements 
    ModelDriven<RegTeacher>  {

    private RegTeacher Teacher= new RegTeacher();
    private List<RegTeacher> alllist=new ArrayList<RegTeacher>();

    /** setter & get **/
    @Override
    public RegTeacher getModel() {
    return Teacher;
    }


    public String execute(){

    boolean i=DaoTeacher.save(Teacher);
    if(i==true)
    {
        return SUCCESS;
    }
    return ERROR;       
    }

    public String Update() {

    boolean i=DaoTeacher.update(Teacher);
    if(i==true)
    {
        return SUCCESS;
    }
    return ERROR;       
    }   

    public String FindById() {
    HttpServletRequest request = (HttpServletRequest) 
    ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
    alllist=DaoTeacher.FindById(request.getParameter("id"));
    System.out.println("working");
    return SUCCESS;
    }
    }`

道老师 这是道课

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新

  `public class DaoTeacher {

    public static boolean save(RegTeacher rs) {
    boolean flag=true;

    SessionFactory factory= new 
    Configuration().configure().buildSessionFactory();
    Session session=factory.openSession();
    Transaction tx=session.beginTransaction();
    try {
        session.save(rs);
        tx.commit();
    }catch(Exception e) {
    flag=false;
        e.printStackTrace();
    }
    session.close();    
    return flag;
    }

    public static boolean update(RegTeacher up) {
    boolean flag=true;

    SessionFactory factory= new 
    Configuration().configure().buildSessionFactory();
    Session session=factory.openSession();

    try {
        Transaction tx=session.beginTransaction();   
        session.update(up);
        tx.commit();
        session.close();    

    }catch(Exception e) {
    flag=false;
        e.printStackTrace();
    }

    return flag;
    }




   public static List<RegTeacher> FindById(String id) {
   SessionFactory factory= new 
   Configuration().configure().buildSessionFactory();
   Session session=factory.openSession();
   Transaction tx=session.beginTransaction();
   List<RegTeacher> list=null;
   RegTeacher reg= new RegTeacher();
   try {
    list=session.createQuery("from RegTeacher r where r.id="+id).list();

    System.out.println("got size"+list.size());

   }catch(Exception e) {
    if (!(tx == null)) {
        tx.rollback();
    e.printStackTrace();}
  }
  tx.commit();
  session.close();
  return list;
  }
  }`

更新教师jsp

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新

 `
 <%@ taglib uri="/struts-tags" prefix="s" %> 
 <s:form action="RegTeacher" method="post" theme="simple" >
 <table>
 <s:iterator value="alllist" >
 <s:hidden name="id" value="%{id}"/>
<tr>
<td>
<s:textfield name="Name" label="Lecturer Name" value="%{Name}"/></td></tr>
<tr>
<td>
<s:textfield name="Email" label="Email" value="%{Email}" /></td></tr>
<tr>
<td>
<s:checkboxlist label="Coures Teaching" list=" 
 {'C','C++','.Net','Python','JavaScript','HTML & CSS','SQL','PHP','Java 
 Core','Adv Java','Android'}" name="CourseT"/></td></tr>
 <tr>
 <td><s:password showPassword="true" name="Password" label="Password" 
 value="%{Password}" /></td></tr>
 <tr>
 <td>
 <s:hidden value="%{#session['name']}" name="CName"/></td></tr>
 </s:iterator>
   <tr><td>
  <s:submit value="Update"></s:submit>  
 </td></tr>
  </table>
 </s:form>`

更新中显示错误

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新
Hibernate: insert into Teacher (ClassName, Course, LecturerName, pass, email) values (?, ?, ?, ?, ?) 18034 [http-nio-8080-exec-1] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000 18034 [http-nio-8080-exec-1] ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry 'Rajan' for key 'LecturerName' org.hibernate.exception.ConstraintViolationException: could not insert: [com.Attendance.RegTeacher] at

休眠文件

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新

`<hibernate-configuration>

 <session-factory>

 <property 
  name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url"> 
   jdbc:mysql://localhost:3306/attendance</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>

    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property 
    name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="hibernate.hbm2ddl.auto">update</property>

  <!-- List of XML mapping files -->
  <mapping class="com.Attendance.RegistrationAdmin"/>
  <mapping class="com.Attendance.DaoAdmin"/>
  <mapping class="com.Attendance.RegTeacher"/>
  <mapping class="com.Attendance.DaoTeacher"/>


   </session-factory>

   </hibernate-configuration> `

请告诉我用strut2 hibernate JPA更新的正确方法关于按id更新

标签: javahibernatejpastruts2

解决方案


从错误中可以清楚地看出Duplicate entry 'Rajan' for key 'LecturerName',您尝试的实体Update被视为重复。原因是每次都调用execute方法来保存实体而不是更新它。TeacherAction

您的问题的解决方案是调用动作类的特定(特定于 jsp 页面)方法。尝试在UpdateTeacher.jsp中添加以下代码行

<s:form action="RegTeacher" method="Update" theme="simple" >

提示:我建议遵循编码标准,使每个人都可以轻松跟踪和理解代码。


推荐阅读