I am trying to save an entity to the database. When I send the post request, I get status:200 ok but the entity is not saved to the database.
Here is the Person class:
Code:
package com.Neo4jRestAPI;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name="gender")
private String gender;
@Column(name="dob")
private String dob;
@Column(name="firstname")
private String firstname;
@Column(name="middlename")
private String middlename;
@Column(name="lastname")
private String lastname;
private String relationship_type;
private String relationship;
public Person() {}
// getters and setters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGender() {
return gender;
}
public String setGender(String gender) {
return this.gender = gender;
}
public String getDob() {
return dob;
}
public String setDob(String dob) {
return this.dob = dob;
}
public String getFirstname() {
return firstname;
}
public String setFirstname(String firstname) {
return this.firstname = firstname;
}
public String getMiddlename() {
return middlename;
}
public String setMiddlename(String middlename) {
return this.middlename = middlename;
}
public String getLastname() {
return lastname;
}
public String setLastname(String lastname) {
return this.lastname = lastname;
}
public String getRelationship_type() {
return relationship_type;
}
public String setRelationship_type(String relationship_type) {
return this.relationship_type = relationship_type;
}
public String getRelationship() {
return relationship;
}
public String setRelationship(String relationship) {
return this.relationship = relationship;
}
}
here is the PersonDAO class:
Code:
import org.hibernate.HibernateException;
import com.google.gson.Gson;
public class PersonDAO {
public void addPerson(Person person){
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(person);
em.flush();
tx.commit();
em.clear();
em.close();
emf.close();
}
catch ( Exception e ) {
e.printStackTrace();
}
}
}
and here is how I try to send the data:
Code:
@POST
@Path("/person")
@Consumes("application/json")
public Response addPerson(Person person){
PersonDAO dao = new PersonDAO();
dao.addPerson(person);
return Response.ok().build();
}
The stack trace does not show any errors, however, when I use log4j, I get the following error:
Code:
"Neo.ClientError.Transaction.TransactionNotFound","message":"Unrecognized transaction id. Transaction may have timed out and been rolled back
When I remove the
Code:
@GeneratedValue
and specify the id in the json, then the entity is saved. When I try to auto generate the id, then the entity is not saved but the id is generated.
Does anyone know what is causing this behavior?