麻烦hibernate的命名查询

时间:2010-07-08 14:00:18

标签: java hibernate orm annotations named-query

我是hibernate的新手,我在命名查询注释方面遇到了一些麻烦。我的代码如下,并且或多或少由NetBeans生成

BasicUser类:

    package wmc.model;

    import java.io.Serializable;
    import javax.persistence.Basic;
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import org.hibernate.annotations.NamedQueries;
    import org.hibernate.annotations.NamedQuery;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;

    @Entity
    @Table(name = "basic_user")
    @NamedQueries({
        @NamedQuery(name = "BasicUser.findAll", query = "SELECT b FROM BasicUser b"),
        @NamedQuery(name = "BasicUser.findByFirstName", query = "SELECT b FROM BasicUser b WHERE b.firstName = :firstName"),
        @NamedQuery(name = "BasicUser.findByLastName", query = "SELECT b FROM BasicUser b WHERE b.lastName = :lastName"),
        @NamedQuery(name = "BasicUser.findByEmail", query = "SELECT b FROM BasicUser b WHERE b.email = :email"),
        @NamedQuery(name = "BasicUser.findByPassword", query = "SELECT b FROM BasicUser b WHERE b.password = :password")})
    public class BasicUser implements Serializable {
        private static final long serialVersionUID = 1L;
        @Basic(optional = false)
        @Column(name = "First_Name")
        private String firstName;
        @Basic(optional = false)
        @Column(name = "Last_Name")
        private String lastName;
        @Id
        @Basic(optional = false)
        @Column(name = "Email")
        private String email;
        @Basic(optional = false)
        @Column(name = "Password")
        private String password;
        @OneToOne(cascade = CascadeType.ALL, mappedBy = "basicUser")
        private StatUser statUser;
        @JoinColumn(name = "Group_Name", referencedColumnName = "Group_Name")
        @ManyToOne(optional = false)
        private Groups groupName;

        public BasicUser() {
        }
...

和hibernate.cfg.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://mysql.dinhost.net:3306/coffeedrinkers</property>
    <property name="hibernate.connection.username">bla</property>
    <property name="hibernate.connection.password">bla</property>
  </session-factory>
</hibernate-configuration>

这是我尝试使用查询的地方:

public static boolean userExists(String email, String password) {
        Session session = null;

          try{

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

            session =sessionFactory.openSession();

            Object object = session.getNamedQuery("wmc.model.BasicUser.findByEmail").
                    setString("email", email).uniqueResult();
            BasicUser user = (BasicUser) object;

            if(user != null && user.getPassword().equals(password)) {
                return true;
            }
          }
          catch(Exception e) {
              e.printStackTrace();
          }

          return false;

    }

据我了解,我不需要制作任何映射xml,因为此信息在注释中。

我感谢任何帮助。提前谢谢你:)

persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
  <persistence-unit name="WMCPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>coffeee</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

和sun-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_coffeedrinkers_AnAmuserPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="mysql.dinhost.net"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="coffeedrinkers"/>
        <property name="User" value="bla"/>
        <property name="Password" value="bla"/>
        <property name="URL" value="jdbc:mysql://mysql.dinhost.net:3306/coffeedrinkers"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" pool-name="mysql_coffeedrinkers_AnAmuserPool" jndi-name="coffeee"  object-type="user"/>
</resources>

2 个答案:

答案 0 :(得分:3)

您命名查询的名称实际上是name,在调用getNamedQuery时不要为其添加前缀

 BasicUser user = (BasicUser) session.getNamedQuery("BasicUser.findByEmail").
         setString("email", email).uniqueResult();

顺便说一句,既然您正在使用JPA注释,那么您应该优先使用JPA API而不是Hibernate API(即JPA的EntityManager而不是Hibernate的Session)。

答案 1 :(得分:0)

如果您想知道如何设置JPA的EntityManager,请参阅:

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="emf"/>

     <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="jpaVendorAdapter">
            <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                    p:showSql="${hibernate.show_sql}"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.books.web.entities"></property>
        <property name="loadTimeWeaver">
         <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
       </property>
   </bean> 

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

拥有bean之后,您可以选择使用注释来获取JPATransactionManager并使用它来获取EntityManagerFactory并使用工厂获取您的EntityManager,然后您可以创建查询/命名查询.etc

在类成员属性之上的某处,使用以下内容自动装配transactionManager:

    @Autowired
    private JpaTransactionManager transactionManager;

public List<Book> findBookByCategory(String category){
        EntityManagerFactory entityManagerFactory = transactionManager.getEntityManagerFactory();
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        List<Book> result = entityManager.createNamedQuery( "Book.findByCategory", Book.class )
                                .setParameter("category", category)
                                .getResultList();
        entityManager.getTransaction().commit();
        entityManager.close();
        return result;
    }

本文顶部的bean引用属性文件中的一些属性,因此请确保您的hibernate.properites文件具有如下值:

hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.show_sql true
hibernate.hbm2ddl.auto update
hibernate.generate_statistics true
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/foobardb?useUnicode=true&amp;createDatabaseIfNotExist=true&amp;characterEncoding=utf-8
hibernate.connection.username put_user_name_here
hibernate.connection.password put_password_here
hibernate.connection.CharSet utf8
hibernate.connection.characterEncoding utf8
hibernate.connection.useUnicode true

要成功创建bean,你需要hibernate-jpa jar,你可能还需要hibernate验证器jar。如果您使用maven来管理项目中的依赖项,请使用以下依赖项:

<dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>${spring.data.jpa.version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>junit-dep</artifactId>
                        <groupId>junit</groupId>
                    </exclusion>
                </exclusions>
            </dependency>

<!--  JPA -->
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>${hibernate.version}</version>
            </dependency>
                        <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>${hibernate.version}</version>
            </dependency>

<dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>${hibernate-validator.version}</version>
                <scope>compile</scope>

                <exclusions>
                    <exclusion>
                        <groupId>javax.xml.bind</groupId>
                        <artifactId>jaxb-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.xml.bind</groupId>
                        <artifactId>jaxb-impl</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

希望这会有所帮助:)