I am trying to save an object using Hibernate and Jersey.
The JSON I'm trying to send is:
{ "firstname":"Jon", "middlename":"J", "lastname":"Smith", "dob":"10-10-1990", "gender":"male" }
When I send it with Postman, I get Status: 200 OK , but the record is not saved in the database.
I am using the Neo4j database.
Here is my PersonDAO class:
package com.Neo4jRestAPI; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; 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(); Person p = new Person(); p.setFirstname(person.getFirstname()); p.setMiddlename(person.getMiddlename()); p.setLastname(person.getLastname()); p.setDob(person.getDob()); p.setGender(person.getGender()); em.persist(p); em.flush(); tx.commit(); em.clear(); em.close(); emf.close(); } catch ( Exception e ) { e.printStackTrace(); } } }
This is how I try to send data:
@POST @Path("/person") @Consumes("application/json") public Response addPerson(Person person){ person.setFirstname(person.getFirstname()); person.setMiddlename(person.getMiddlename()); person.setLastname(person.getLastname()); person.setDob(person.getDob()); person.setGender(person.getGender()); PersonDAO dao = new PersonDAO(); dao.addPerson(person); return Response.ok().build(); }
Does anyone know what I'm doing wrong here?
EDIT
I managed to save the object using native query , but in this case id will not be automatically generated. I still cannot save the object with the method described above
When I delete @GeneratedValue and specify id in JSON, I can save the object, so I assume there is a problem. I tried several strategies, but none of them worked.
This is how I try to auto generate id :
@Entity @Table(name="Person") public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id;
Also, when I print the getId() method, I get an automatically incrementing value.
Here is the Cypher request that was executed:
"{"statements":[{"statement":"CREATE (n:ENTITY:Person {props}) RETURN n","parameters":{"props":{"firstname":"Jon","gender":"male","dob":"10-10-1990","middlename":"J","id":99,"lastname":"Smith"}},"includeStats":false,"resultDataContents":["graph"]}]}"
I also get a transaction rollback error, but it does not say why it was rolled back:
"Neo.ClientError.Transaction.TransactionNotFound","message":"Unrecognized transaction id. Transaction may have timed out and been rolled back
Here is my persistence.xml file
<?xml version="1.0"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="persistence"> <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider> <properties> <property name="hibernate.ogm.datastore.provider" value="neo4j_http"/> <property name="hibernate.ogm.neo4j.database_path" value="C://path//to//database"/> <property name="hibernate.ogm.datastore.host" value="localhost:7474"/> <property name="hibernate.ogm.datastore.username" value="neo4j"/> <property name="hibernate.ogm.datastore.password" value="root"/> </properties> </persistence-unit> </persistence>
EDIT
This is the stack trace I get:
log4j:WARN No appenders could be found for logger (org.jboss.logging). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html
When I use log4j and add BasicConfiguratior.configure() , I get the following (here is quite a lot of text to publish the entire log, so I just placed its part where the error was selected):
2342 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "{"statements":[{"statement":"CREATE (n:ENTITY:Person {props}) RETURN n","parameters":{"props":{"firstname":"Anna","gender":"female","dob":"10-10-1990","middlename":"J","id":57,"lastname":"Smith"}},"includeStats":false,"resultDataContents":["graph"]}]}" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "HTTP/1.1 200 OK[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Date: Tue, 03 Oct 2017 09:01:10 GMT[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Content-Type: application/json[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Access-Control-Allow-Origin: *[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Content-Length: 372[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Server: Jetty(9.2.z-SNAPSHOT)[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "[\r][\n]" 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 200 OK 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << HTTP/1.1 200 OK 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Date: Tue, 03 Oct 2017 09:01:10 GMT 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Content-Type: application/json 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Access-Control-Allow-Origin: * 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Content-Length: 372 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Server: Jetty(9.2.z-SNAPSHOT) 2345 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely 2346 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "{"commit":"http://localhost:7474/db/data/transaction/53/commit","results":[{"columns":["n"],"data":[{"graph":{"nodes":[{"id":"10","labels":["ENTITY","Person"],"properties":{"firstname":"Anna","gender":"female","dob":"10-10-1990","middlename":"J","id":57,"lastname":"Smith"}}],"relationships":[]}}]}],"transaction":{"expires":"Tue, 03 Oct 2017 09:02:10 +0000"},"errors":[]}" 2346 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection [id: 1][route: {}->http://localhost:7474] can be kept alive indefinitely 2346 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection released: [id: 1][route: {}->http://localhost:7474][total kept alive: 1; route allocated: 2 of 10; total allocated: 2 of 10] 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - committing 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Processing flush-time cascades 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Dirty checking collections 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.internal.util.EntityPrinter - Listing entities: 2350 [http-nio-8080-exec-4] DEBUG org.hibernate.internal.util.EntityPrinter - com.Neo4jRestAPI.Person{firstname=Anna, gender=female, relationship_type=null, dob=10-10-1990, middlename=J, id=57, relationship=null, lastname=Smith} 2350 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection request: [route: {}->http://localhost:7474][total kept alive: 1; route allocated: 2 of 10; total allocated: 2 of 10] 2350 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection leased: [id: 1][route: {}->http://localhost:7474][total kept alive: 0; route allocated: 2 of 10; total allocated: 2 of 10] 2350 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.client.DefaultHttpClient - Stale connection check 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: best-match 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.client.DefaultHttpClient - Attempt 1 to execute request 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Sending request: POST /db/data/transaction/54/commit HTTP/1.1 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "POST /db/data/transaction/54/commit HTTP/1.1[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "Accept: application/json[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "Accept-Encoding: gzip, deflate[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "Authorization: Basic bmVvNGo6Z2VuaXZpdHk=[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "X-Stream: true[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "Content-Length: 0[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "Host: localhost:7474[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - >> "[\r][\n]" 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> POST /db/data/transaction/54/commit HTTP/1.1 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> Accept: application/json 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> Accept-Encoding: gzip, deflate 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> Authorization: Basic bmVvNGo6Z2VuaXZpdHk= 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> X-Stream: true 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> Content-Length: 0 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> Host: localhost:7474 2352 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - >> Connection: Keep-Alive 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "HTTP/1.1 404 Not Found[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Date: Tue, 03 Oct 2017 09:01:10 GMT[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Content-Type: application/json[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Access-Control-Allow-Origin: *[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Content-Length: 178[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "Server: Jetty(9.2.z-SNAPSHOT)[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "[\r][\n]" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 404 Not Found 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << HTTP/1.1 404 Not Found 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Date: Tue, 03 Oct 2017 09:01:10 GMT 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Content-Type: application/json 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Access-Control-Allow-Origin: * 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Content-Length: 178 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.headers - << Server: Jetty(9.2.z-SNAPSHOT) 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.client.DefaultHttpClient - Connection can be kept alive indefinitely 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.wire - << "{"results":[],"errors":[{"code":"Neo.ClientError.Transaction.TransactionNotFound","message":"Unrecognized transaction id. Transaction may have timed out and been rolled back."}]}" 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection [id: 1][route: {}->http://localhost:7474] can be kept alive indefinitely 2354 [http-nio-8080-exec-4] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection released: [id: 1][route: {}->http://localhost:7474][total kept alive: 1; route allocated: 2 of 10; total allocated: 2 of 10] 2355 [http-nio-8080-exec-4] DEBUG org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl - Initiating JDBC connection release from afterTransaction 18718 [Finalizer] DEBUG org.apache.http.wire - << "{"password_change_required":false,"password_change":"http://localhost:7474/user/neo4j/password","username":"neo4j"}" 18719 [Finalizer] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection [id: 0][route: {}->http://localhost:7474] can be kept alive indefinitely 18719 [Finalizer] DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager - Connection released: [id: 0][route: {}->http://localhost:7474][total kept alive: 2; route allocated: 2 of 10; total allocated: 2 of 10]
Also, another thing that I noticed is that when I send JSON for the first time, I get the log as shown above, after I send it for the second time, each line is printed twice when I send its third time, each line is printed 3 times, etc.
I'm not sure what this causes, but it can be the cause of the problem.