Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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 更新方法struts 2-hibernate-jpa_Java_Hibernate_Jpa_Struts2 - Fatal编程技术网

Java 更新方法struts 2-hibernate-jpa

Java 更新方法struts 2-hibernate-jpa,java,hibernate,jpa,struts2,Java,Hibernate,Jpa,Struts2,struts.xml 这是Struts类 请告诉我strut2 hibernate JPA的正确更新方式 关于按id更新 ` 德黑兰行动 这是动作课 请告诉我strut2 hibernate JPA的正确更新方式 关于按id更新 ` package com.Attendance; import javax.persistence.*; @Entity @Table(name="Teacher") public class RegTeacher{ @Id

struts.xml 这是Struts类

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

德黑兰行动 这是动作课

请告诉我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 **/
  }`
  `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;
    }
    }`
  `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;
  }
  }`
 `
 <%@ 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>`
`<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更新

从键“讲师姓名”的错误重复条目“Rajan”中可以清楚地看出,您尝试更新的实体被视为重复实体。作为TeacherAction的执行方法的原因是每次调用时都会保存实体,而不是更新实体

  ` 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 **/
  }`
  `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;
    }
    }`
  `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;
  }
  }`
 `
 <%@ 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>`
`<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> `
您的问题的解决方案是调用action类的jsp页面方法的SpecialSpecific。尝试在您的应用程序中添加以下代码行 updateTech.jsp


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

我将execute方法更改为save方法,但不使用相同的错误不要更改Action类中的任何方法。只需添加到UpdateTech.jsp页面,我感觉您想知道使用strut2 hibernate JPA更新的正确方法,关于id更新。