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'.