Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Rest status code: 200 but entity not saved

I am trying to save an entity using Hibernate and Jersey.

The JSON that I try 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.

The database that I am using is Neo4j.

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 the 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 am doing wrong here?

EDIT

I was able to save an entity using native query but that way, the id is not automatically generated. I am still unable to save an entity with the way described above

When I remove the @GeneratedValue and specify the id in the JSON, then I am able to save the entity, so I assume the problem is there. I have tried several strategies but none of them worked.

This is how I try to auto-generate the id:

@Entity
@Table(name="Person")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

Also, when I print the method getId(), I get the auto-incremented value.

Here is the Cypher query that has been 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 am also getting a transaction rollback error but it does not say why it has been 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 that I am getting:

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#noconfig for more info. 

When I use log4j and add BasicConfiguratior.configure(), I get the following (it is quite big to post the whole log here, so I just posted a part of it where the error is thrown):

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 the JSON for the first time, I get the log like above after I send it for the second time, every line gets printed twice when I send it for the third time, every line gets printed 3 times, and so on...

I am not sure what is causing that but it might be the cause of the problem

like image 345
Porjaz Avatar asked Oct 30 '22 01:10

Porjaz


1 Answers

I was able to solve my problem by changing from tomcat 8.5 to wildfly 9. I am still not sure what was the problem with tomcat though, since all the other operations worked fine, just the insertion of people was causing problems.

like image 126
Porjaz Avatar answered Nov 06 '22 20:11

Porjaz