3

我是休眠的新手,我在命名查询注释方面遇到了一些问题。我的代码如下,或多或少是由 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,因为此信息在注释中。

我很感激任何帮助。先感谢您:)

持久性.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>
4

2 回答 2

3

你命名查询的名字真的是它的名字,name调用的时候不要加前缀getNamedQuery

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

顺便说一句,由于您使用的是 JPA 注释,因此您应该更喜欢 JPA API 而不是 Hibernate API(即 JPAEntityManager优于 Hibernate 的Session)。

于 2010-07-08T14:35:49.290 回答
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>

希望这可以帮助 :)

于 2014-09-21T17:43:32.423 回答