如何使用hibernate,jpa从db检索数据

时间:2013-10-06 16:20:22

标签: playframework

我正在使用play-2.1.3框架。我想从DB中检索数据。我正在使用eclipse,hibernate和postgreSQL。

当我想使用实体mgr从数据库中检索数据时,它会给我一个错误,我找不到答案...请帮帮我。

----------------------日志文件中的错误--------------------- -----

2013-10-06 19:08:53,729 - [ERROR] - from org.hibernate.engine.jdbc.spi.SqlExceptionHelper in play-akka.actor.default-dispatcher-3 
ERROR: column client0_._ebean_intercept does not exists
  Position: 31

我的实体类:

package models;
import play.db.ebean.*;
import play.data.validation.*;
import javax.persistence.*;
@Entity
@Table(name="clients")
public class Client extends Model {


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Constraints.Min(10)
    public Long id;

  @Constraints.Required
  public String username;

  @Constraints.Required
  public String email;

  @Constraints.Required
  public String password;

  @Constraints.Required
  public String passwordsignup_confirm;

  public boolean online;


  public boolean getOnline() {
        return online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirm() {
        return passwordsignup_confirm;
    }

    public void setConfirm(String confirm) {
        this.passwordsignup_confirm= confirm;
    }



}

------------------------------在我的Controller类中---------

public static Client ckeckEmailAndPassword(String email, String password)
    {
        Client client = null;
        em.getTransaction().begin();

            try
            {
                List<Client> cli = em.createQuery("SELECT d FROM Client d WHERE d.email = ?1 AND d.password = ?2").setParameter(1, email).setParameter(2, password).getResultList();

                em.getTransaction().commit();

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

    }

-------------------------------------- Build.scala

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

  val appName         = "unity"
  val appVersion      = "1.0-SNAPSHOT"

  val appDependencies = Seq(
    // Add your project dependencies here,

    javaCore,
    javaJdbc,
    javaEbean,
    "org.hibernate" % "hibernate-entitymanager" % "4.2.5.Final"
  )

  val main = play.Project(appName, appVersion, appDependencies).settings(
      ebeanEnabled := false      
  )





}

------------------------------------------的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="manager" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        </properties>
    </persistence-unit>


</persistence>

----------------------------------------------- --application.conf

 db.default.driver=org.postgresql.Driver
 db.default.url="jdbc:postgresql:postgres"
 db.default.user=postgres
 db.default.password="root"

 db.default.jndiName=DefaultDS

1 个答案:

答案 0 :(得分:1)

你必须使用配置文件(application.conf)给你。

enter code here
# Database configuration
# ~~~~~ 
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/nameof_you_DB"
db.default.user=your_user_name
db.default.password=you_password