gpt4 book ai didi

java - 如何使用 JPA 和 Hibernate 通过自定义对象实体属性进行查询

转载 作者:行者123 更新时间:2023-11-30 06:16:26 25 4
gpt4 key购买 nike

编写一个 DAO 方法以通过电子邮件检索 appUsers,其中电子邮件属于 EmailAddress 类。一直在寻找如何做到这一点无济于事。有没有办法根据自定义对象的值检索记录?当我有一个 String 或 int 类型的属性时,这似乎有效,但我如何获得与我的 EmailAddress 对象匹配的记录?

尝试了以下但不工作:

public List<AppUser> findByEmail(EmailAddress email) {
log.debug("finding " + getTable() + " instance by example");

AppUser appUser = new AppUser();
appUser.setEmail(email);

try {
List<AppUser> results = this.sessionFactory.getCurrentSession()
.createCriteria(AppUser.class)
.add(Example.create(appUser)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

这是我的 AppUser 类的相关部分

@Entity
@Table(name="AppUser")
public class AppUser extends BaseEntity {
...

@Column(name="Email")
private EmailAddress email;

...

/**
* Gets this AppUser's associated email
*
* @return this AppUser's associated email
*/
public EmailAddress getEmail() {
return this.email;
}

/**
* Sets this AppUser's associated email
*
* @param email this AppUser's associated email
*/
public void setEmail(EmailAddress email) {
this.email = email;
}
}

这是我的 EmailAddress 类

  public class EmailAddress implements Serializable {
private static final long serialVersionUID = -6999956021169014445L;
private static final String AT_DELIMTER = "@";
private String emailAddress;

public EmailAddress() {}

public EmailAddress(String emailAddress) {
setEmailAddress(emailAddress);
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) throws IllegalArgumentException {
if (StringUtils.isEmpty(emailAddress))
throw new IllegalArgumentException("Parameter emailAddress cannot be blank.");
else if (!EmailValidator.getInstance(true).isValid(emailAddress))
throw new IllegalArgumentException("The email address " + emailAddress + " is not valid.");

this.emailAddress = emailAddress;
}

public String getUser() {
return this.emailAddress.substring(0, this.emailAddress.indexOf(AT_DELIMTER));
}

public String getDomain() {
return this.emailAddress.substring(this.emailAddress.indexOf(AT_DELIMTER) + 1);
}

public String toString() {
return emailAddress;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((emailAddress == null) ? 0 : emailAddress.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EmailAddress other = (EmailAddress) obj;
if (emailAddress == null) {
if (other.emailAddress != null)
return false;
} else if (!emailAddress.equals(other.emailAddress))
return false;
return true;
}
}

谢谢!

最佳答案

将您的查询更改为:

List results = this.sessionFactory.getCurrentSession()
.createQuery("select au from AppUser au where au.email = :email")
.setParameter("email", email)
.list();

您需要实现 Hibernate custom UserType在加载时将数据库字符串转换为 EmailAddress。

使用 ImmutableType来自 hibernate-types 项目作为自定义 Hibernate Type 的基类,因为这样实现它要容易得多。

关于java - 如何使用 JPA 和 Hibernate 通过自定义对象实体属性进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27284228/

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