我有以下
package model.database;
import static javax.persistence.GenerationType.IDENTITY;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table( name = "DB_ACCOUNT_BILLING" )
public class AccountBilling implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private long userId;
private String firstName;
private String lastName;
private String username;
private String password;
public AccountBilling()
{
}
@Id
@GeneratedValue( strategy = IDENTITY )
@Column( name = "USER_ID", unique = true, nullable = false )
public long getUserId()
{
return userId;
}
public void setUserId( long userId )
{
this.userId = userId;
}
@Column( name = "FIRST_NAME", unique = false, nullable = false, length = 20 )
public String getFirstName()
{
return firstName;
}
public void setFirstName( String firstName )
{
this.firstName = firstName;
}
@Column( name = "LAST_NAME", unique = false, nullable = false, length = 20 )
public String getLastName()
{
return lastName;
}
public void setLastName( String lastName )
{
this.lastName = lastName;
}
@Column( name = "USERNAME", unique = true, nullable = false, length = 20 )
public String getUsername()
{
return username;
}
public void setUsername( String username )
{
this.username = username;
}
@Column( name = "PASSWORD", unique = false, nullable = false, length = 20 )
public String getPassword()
{
return password;
}
public void setPassword( String password )
{
this.password = password;
}
}
我想将用户名作为二级索引,但我不确定应该用什么Hibernate注释来指定它。此外,目前用于主索引检索,我做了类似的事情:
session.beginTransaction();
AccountBilling accountBilling = (AccountBilling) session.get( AccountBilling.class, (long) primaryIndex );
但我怎样才能进行二级索引检索?
非常感谢任何帮助/建议。
答案 0 :(得分:1)
要使Hibernate创建索引,您可以使用注释... Index
。
要通过userName检索AccountBilling,您将使用查询:
String hql = "select a from AccountBilling a where a.userName = :userName";
AccountBilling result = session.createQuery(hql)
.setString("userName", userName)
.uniqueResult();