Separate entity for primary keys
I have two tables as below.
"User Acc" is user's profile details and user's login details(username password) are in a seperate table called login.When I generated the entities in Netbeans IDE, there were 2 tables for login(database) table. One generated entity is "Login", another one is "LoginId".Login entity has a reference to LoginId and UserAcc entity. LoginId entity has username and password which were in login database table.UserAcc has primary key which is INT for each user.That key is the foreign key of login table.
Now I want to check the user @ loging. How I do is, I create a LoginId instance and set the user name and password and then check any object which has same LoginId. Here is the code.
uName and pw are strings and taken from user input. LoginId id=new LoginId(uName, pw);
Session ses = NewHibernateUtil.getSessionFactory().openSession();
Criteria crit = ses.createCriteria(Login.class);
crit.add(Restrictions.eq("id", id));
Entities.Login log = (Entities.Login)crit.uniqueResult();
But eventhough there are existing usernames and passwords matching with given username and password, the log
is always null.
Any one tell me where I should check and what the problem is. Thank you. And my other problem is why a separate entity("LoginId") is created for two columns(primary keys) in one database table and another entity for that its database table .
for login;
<hibernate-mapping>
<class name="Entities.Login" table="login" catalog="pcw">
<composite-id name="id" class="Entities.LoginId">
<key-property name="uname" type="string">
<column name="uname" length="10" />
</key-property>
<key-property name="pw" type="string">
<column name="pw" length="10" />
</key-property>
</composite-id>
<many-to-one name="useracc" class="Entities.Useracc" fetch="select">
<column name="UserAcc_uid" not-null="true" />
</many-to-one>
</class>
for UserAcc:
<hibernate-mapping>
<class name="Entities.Useracc" table="useracc" catalog="pcw">
<id name="uid" type="java.lang.Integer">
<column name="uid" />
<generator class="identity" />
</id>
<property name="fname" type="string">
<column name="fname" length="45" />
</property>
<property name="sname" type="string">
<column name="sname" length="45" />
</property>
<property name="RDate" type="date">
<column name="r_date" length="10" />
</property>
<property name="street" type="string">
<column name="street" length="45" />
</property>
<property name="city" type="string">
<column name="city" length="45" />
</property>
<property nam开发者_StackOverflow社区e="email" type="string">
<column name="email" length="45" />
</property>
<property name="tel" type="string">
<column name="tel" length="45" />
</property>
<set name="comments" inverse="true" cascade="all">
<key>
<column name="UserAcc_uid" not-null="true" />
</key>
<one-to-many class="Entities.Comment" />
</set>
<set name="logins" inverse="true" cascade="all">
<key>
<column name="UserAcc_uid" not-null="true" />
</key>
<one-to-many class="Entities.Login" />
</set>
</class>
Anyway you can use HQL to solve it it will look more and less like this:
public boolean authenticate(Sting username,String pass){
Session ses = NewHibernateUtil.getSessionFactory().openSession();
String sql = "from Login login where login.username:= username and login.pass = :=pass";
Query query = session.createQuery(sql);
query.setString("username",username);
query.setString("pass",pass);
List<Login> result = query.list();
//close session, transaction,etc....
if (result ==null)
return false;
else
return true;
}
精彩评论