简体   繁体   中英

Play framework - can not connect to postgresql

I start to use postgresql in my play app and able to connect to play's default database and do some select and update. But there re always errors when I turn to use postgresql. Please give me some advice.Thank you. I already added postgresql-42.0.0.jar in /lib.

error in this line:"u1.save();": [PersistenceException: Error getting sequence nextval]

    application.conf:
    db.default.driver=org.postgresql.Driver
    db.default.url="postgres://postgres:123456@localhost/testing"
    #db.default.url="postgres://user:password@servername/dataBaseName"
    # Ebean configuration
    ebean.default="models.*"

    build.sbt:
    libraryDependencies ++= Seq(
    "org.postgresql" % "postgresql" % "42.0.0 JDBC 42",
    javaJdbc,
    javaEbean,
    cache
) 

    Application.java:

public class Application extends Controller {
    public static Result index() {
        User u1 = new User();
        User u2 = new User();
        u1.name = "Mm";
        u2.name = "Nn";
        u1.save();
        u2.save();
        return ok("saved");
    }
    public static Result allUser() {
        List<User> users =  User.findAll();
        return ok(findalluser.render(users));
    }
}

If your error is really [PersistenceException: Error getting sequence nextval] it means that you did not create correctly your sequence generator that is responsible to increment automatically your id`s.

On your model you gotta annotate your class with this annotation on your primary key:

@Entity
public class User extends Model {
    @Id //makes it a primary key
    @GeneratedValue //creates the sequence generator
    public Long id;
}

我认为应该有端口号:

 db.default.url="postgres://postgres:123456@localhost:5432/testing"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM