java - EntityManager.persist() not inserting values to DB -


New to JPA and Servlets. I am trying to create a student unit and using a servicelet to convert your DB I am trying to put There are no errors in the code when I go to the servlet, the browser worked without errors but the prices are not putting my DB

Student Unit: -

  @Entity applies to the public class student Serializable {Personal Stable last long serialVersionUID = 1L; @ Id @ Generated Values ​​(strategy = generation type.eeto) Private long id; @column (drain = false) name of private string; @column (negligible = false) Private interval age; Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; } Public int getAge () {return age; } Public Zero setAge (int age) {this.age = age; } Public Long IID () {Return ID; } Public Zero Set ID (Long ID) {this.id = id; } @ Override Public Ent Hashod () {Inti hash = 0; Hash + = (id! = Tap? Id.hashCode (): 0); Return hash; } @ Override public boolean equals (Object object) {// TODO: Warning - this method ID field will not be set (student!} Student Other = object (student); if ((this.id == NULL And other; id! = Null) || (this.id! = Null & amp; this.id.equals (other.id))) {return false;} back true;} @ override public string toasting ( } {Return "model. Student [id =" + id + "]";}  

}

My servlet: -

 < Code> public class A spreads HttpServlet {@PersistenceUni (UnitName = "persistenceCheckPU") EntityManagerFactory emf; @Resource UserTransaction ut; Protects Zero ProcessRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ("text / html; charset = UTF-8"); try to (Printwardr out = response .getWriter ()) {student = new student (); student.setName ( "John"); student.setAge (22); EntityManager em = emf.createEntityManager (); ut.begin () ; Em.persist (student); Ut.commit (); Out.println ("& lt; h2> success !!! "); } Catch (SecurityException | IllegalStateException | javax.transaction.RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException | NotSupportedException ex) {Logger.getLogger (A.class.getName ()) log (Level.SEVERE, null, East). }}  

My purpose: -

  • Create ID as unique and auto increment.

Renewed

persistence.xml

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Persistence version = "2.1" xmlns = "http://xmlns.jcp.org/xml/ns/produstence" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" XSI: Schema Locations = "Http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> & Lt; Firmness unit name = "persistence check pu" transaction type = "jta" & gt; & Lt; JTA-Data-Source & gt; Jdbc_persistence & lt; / JTA-Data-Source & gt; & Lt; Will be unlisted sections & gt; False & lt; / Will be unlisted sections & gt; & Lt; Properties & gt; & Lt; Property Name = "javax.persistence.schema-generation.database.action" value = "Create" /> & Lt; / Properties & gt; & Lt; / Persistence unit & gt; & Lt; / Persistence>  

Check that you have a JTA firmness unit ( persistence.xml < / Code>) and then em.joinTransaction () call to the start or after the transaction after EntityManager em = emf.createEntityManager (); Call .


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -