更新方法struts 2-hibernate-jpa

时间:2019-03-06 15:04:29

标签: java hibernate jpa struts2

struts.xml 这是Struts类

请告诉我使用strut2休眠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休眠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 这是动作课

请告诉我使用strut2休眠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;
    }
    }`

DaoTeacher 这是道课

请告诉我使用strut2休眠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;
  }
  }`

Upadate老师jsp

请告诉我使用strut2休眠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休眠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休眠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休眠JPA更新的正确方法 关于通过ID

更新

1 个答案:

答案 0 :(得分:1)

从错误Duplicate entry 'Rajan' for key 'LecturerName'可以很明显地看出,您尝试Update的实体被视为重复实体。每次都会调用作为execute的{​​{1}}方法的原因,这将保存实体而不是对其进行更新。

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

TeacherAction

提示::我建议遵循编码标准,该标准使每个人都易于跟踪和理解代码。