gpt4 book ai didi

java - hibernate 的命名查询有问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:03:39 25 4
gpt4 key购买 nike

我是 hibernate 的新手,我在使用命名查询注释时遇到了一些问题。我的代码如下,或多或少是由 NetBeans 生成的

基本用户类:

    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>

最佳答案

你命名查询的名称就是它的名称,调用getNamedQuery时不要加前缀

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

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

关于java - hibernate 的命名查询有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3204328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com