3

I have the following

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

}

and I would like to make username as a secondary index, but I'm not sure what Hibernate annotation I should use to designate that. Also, currently for primary index retrieval, I do something like:

    session.beginTransaction();

    AccountBilling accountBilling = (AccountBilling) session.get( AccountBilling.class, (long) primaryIndex );

but how would I be able to do secondary index retrieval?

Any help/suggestion will be much appreciated.

1 Answer 1

1

To make Hibernate create an index, you would use the annotation... Index.

To retrieve an AccountBilling by userName, you would use a query:

String hql = "select a from AccountBilling a where a.userName = :userName";
AccountBilling result = session.createQuery(hql)
                               .setString("userName", userName)
                               .uniqueResult();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. Does this mean creating an index only makes the query faster? Normally you could still query for an column that is not indexed, correct?
Yes, that's what an index is used for. Remember that doing session.get() will also generate a SQL query behind the scene.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.