1

How should I insert data on postgers database using java? Here is the code I'm executing, which throws me an error:

public static Result add() {
    response().setContentType("application/json");
    JsonNode val = request().body().asJson();
    System.out.println(val);
    ObjectNode result= new ObjectNode(val);
    String firstname=val.findPath("firstname").textValue();
    System.out.println(firstname);
    String lastname=val.findPath("lastname").textValue();
    System.out.println(lastname);
    result.put(firstname, 1);
    result.put(lastname, 2);
    System.out.println(result);
    Ebean.save(result);
    return ok(index.render("Record has been inserted"+user));
}
2
  • You could use plain JDBC for persistence of data. JPA and Spring JPA are other options. Commented Jun 24, 2014 at 10:20
  • Which error are you getting? Commented Jun 24, 2014 at 10:23

2 Answers 2

0
public static Result add() throws Exception {
    Myapps user = new Myapps();
    try {
        JsonNode requestBody = request().body().asJson();
         ObjectMapper mapper = new ObjectMapper();

                     ObjectReader reader = mapper.readerForUpdating(user);

                     user = reader.readValue(requestBody);
        user.save();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return ok(Json.toJson("inserted value is" + user));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Play uses Ebean (by default) as the persistence framework which uses JPA for mapping entities. See the docs for more. Therefore you can only persist JPA annotated entities. You are trying to persist a Jackson ObjectNode object which is obviously not a JPA annotated entity.

Assuming what you are trying to persist is a Person, your code would change to the following:

app/models/Person.java

package models;

import play.db.ebean.Model;

import javax.annotation.Generated;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;
import javax.persistence.Id;

@Entity
public class Person extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_seq_gen")
    @SequenceGenerator(name = "person_seq_gen", sequenceName = "person_id_seq")
    public Long id;

    public String firstName;
    public String lastName;

}

In your controller class:

public static Result add() {
    JsonNode val = request().body().asJson();
    System.out.println(val);

    String firstname=val.findPath("firstname").textValue();
    System.out.println(firstname);
    String lastname=val.findPath("lastname").textValue();
    System.out.println(lastname);

    Person person = new Person();
    person.firstName = firstname;
    person.lastName = lastname;
    person.save();

    return ok(index.render("Record has been inserted"+person));
}

NOTE You will have to configure your jdbc driver for Postgres as well. See here.

This code will persist your "person" record into a table called 'Person'.

2 Comments

i tried this,each time i compile the record will inserted in db,incase i refresh the page and insert means it throw error
@user3759092 Have a look at this answer and adjust the annotation on Person.id as indicated: stackoverflow.com/a/13107516/2408961

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.