3

我有以下

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 );

但是我怎么能做二级索引检索呢?

任何帮助/建议将不胜感激。

4

1 回答 1

1

要让 Hibernate 创建索引,您可以使用注解Index...。

要按用户名检索 AccountBilling,您可以使用查询:

String hql = "select a from AccountBilling a where a.userName = :userName";
AccountBilling result = session.createQuery(hql)
                               .setString("userName", userName)
                               .uniqueResult();
于 2013-01-07T22:32:17.763 回答