-First of all, because you are a novice, now is also just contact with the three major Java framework so there may be some problems I hope you can point out to me, I will humbly accept.
First of all, I use the annotation of the hibernate to achieve the first hibernate annotation environment, I used reverse engineering to remove the entity class and its main configuration file Hibernate.cfg.xml file, and then Hbm.xml files that were generated at the same time, adding Current_session_context_ in the properties above The class value for thread is primarily for the subsequent session acquisition.
then write the configured properties in the entity class for example
@Entity @Table (name= "Types") public class types implements Java.io.Serializable {//Fields @Id @GeneratedVal UE (generator= "Seqs", Strategy=generationtype.sequence) @SequenceGenerator (name= "Seqs", sequencename= "seq_id",
allocationsize=1) private Integer ID;
@Column private String name;
@OneToMany (mappedby= "types") private set
public void Validatelogin () {
if (Username.trim (). Length () ==0)
{
this.addfielderror ("ErrorName", "User name cannot be empty");
if (Password.trim (). Length () ==0)
{
this.addfielderror ("errorpwd", "Password cannot be null");
}
-Implement data access to the DAO layer
here's how to get a session:
Static Sessionfactory Sf=null;
static {
Annotationconfiguration CF = new annotationconfiguration (). Configure ();
Sf=cf.buildsessionfactory ();
}
public static Sessions GetSession () {return
sf.getcurrentsession ();
}
> Here is the login data access code due to the way the session is accessed so must open things.
Public Users Login (String username,string password) throws Exception
{
Users U=null;
Transaction Tx=null;
Session S=null;
try {
S=hibernateutil.getsession ();
Tx=s.begintransaction ();
String hql= "from Users where name=?" and password=? ";
Query query=s.createquery (HQL);
Query.setstring (0, username);
Query.setstring (1,password);
u= (Users) Query.uniqueresult ();
Map Session=actioncontext.getcontext (). GetSession ();
Session.put ("username", u.getusername ());
Tx.commit ();
catch (Exception e) {
throw new Exception (e);
}
return u;
}
"'
Registration needs to be noted in the action to encapsulate it into the user class at the data access layer
The code follows, of course, in the action should implement the registered checksum.
' Public Integer register (Users u) throws exception{
int result=0;
Session s=hibernateutil.getsession ();
Transaction tx=s.begintransaction ();
try {result= (Integer) s.save (U);
Tx.commit ();
catch (Exception e) {throw new Exception (e);
return result; }