Reputation: 1053
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.
Upvotes: 3
Views: 749
Reputation: 691755
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();
Upvotes: 1