Squeryl没有从数据库中获取数据

时间:2018-12-14 14:44:21

标签: postgresql scala sbt squeryl

我准备了一个squeryl示例,并尝试使用postgres运行它。

    package code
import org.squeryl._
import PrimitiveTypeMode._
import org.squeryl.adapters.PostgreSqlAdapter
object Main {
  def main(args: Array[String]) {
    Class.forName("org.postgresql.Driver");
    SessionFactory.concreteFactory = Some(()=>
      Session.create(
        java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/orders", "pos", "pos"),
        new PostgreSqlAdapter)
    )
    inTransaction {
      import Library._

      drop  // Bad idea in production application!!!!
      create
      printDdl

      authors.insert(new Author(1, "JRR", "Tolkien", None))
      authors.insert(new Author(2, "Jane", "Austen", None))
      authors.insert(new Author(3, "Philip", "Pullman", None))

      books.insert(new Book(1, "The Lord of the Rings", 1, None))
      books.insert(new Book(2, "Pride and Prejudice", 2, None))
      books.insert(new Book(3, "His Dark Materials", 3, None))

      val q = from(books)((b) =>
        select(b)
      )

      for ((book) <- q) {
        println(" wrote " + book.title)
      }
    }
  }
}

class Author(val id: Long,
             val firstName: String,
                 val lastName: String,
                 val email: Option[String]) extends KeyedEntity[Long]

class Book(val id: Long,
           val title: String,
           val authorId: Long,
           val coAuthorId: Option[Long]) extends KeyedEntity[Long]

object Library extends Schema {
  val authors = table[Author]
  val books = table[Book]
}

输出为

    Running code.Main Main
-- table declarations :
create table "Author" (
    "email" varchar(128),
    "lastName" varchar(128) not null,
    "firstName" varchar(128) not null,
    "id" bigint primary key not null
  );
create sequence "s_Author_id";
create table "Book" (
    "id" bigint primary key not null,
    "authorId" bigint not null,
    "coAuthorId" bigint,
    "title" varchar(128) not null
  );
create sequence "s_Book_id";
 wrote 
 wrote 
 wrote 
[success] Total time: 9 s, completed Dec 14, 2018 4:38:05 PM

它正在获取数据库中的行数,但是会显示默认值,同时它应该显示数据库中的书名。 我还尝试创建一个默认实例,但输出没有变化。

我浪费了太多时间寻找任何解决方案,但找不到任何解决方案。

0 个答案:

没有答案