.


:




:

































 

 

 

 





, mapping- ConnectionFactory. , hibernate, autoReconnect=true, , , .

/* # 8: mapping :

ConnectionFactory.java */

package com.hib;

import net.sf.hibernate.HibernateException;

import net.sf.hibernate.MappingException;

import net.sf.hibernate.Session;

import net.sf.hibernate.SessionFactory;

import net.sf.hibernate.cfg.Configuration;

import java.util.Date;

 

public class ConnectionFactory {

public static long timer = 0;

public static SessionFactory sessionFactory = null;

 

public static void initConnectionFactory() {

Date dt = new Date();

timer = dt.getTime();

try {

// mapping-

Configuration cfg = new Configuration().addClass(Student. class).addClass(Course. class);

 

//

sessionFactory = cfg.buildSessionFactory();

} catch (MappingException e) {

System.err.print(e);

} catch (HibernateException e) {

System.err.print(e);

destroy();

}

}

public static Session getOrInitSession() {

try {

Date curDate = new Date();

long curTime = curDate.getTime();

long tenminutes = 10 * 60 * 1000;

 

if (curTime - timer > tenminutes){

destroy();

}

else {

curDate = new Date();

timer = curDate.getTime();

}

if (sessionFactory == null) {

initConnectionFactory();

}

return sessionFactory.openSession();

 

} catch (HibernateException e) {

System.err.print(e);

destroy();

return null;

}

}

public static void destroy() {

timer = 0;

try {

// , ..

sessionFactory.close();

} catch (Exception e) {

e.printStackTrace();

}

sessionFactory = null;

}

}

. 10 , . . Hibernate , hbm.xml , .

, c Hibernate, JDBC MySQL MySQL.

.

/* # 9: mapping : StudentDAO.java */

package com.hib;

import net.sf.hibernate.*;

public class StudentDAO {

//

public static Boolean studentExists(String login) {

Session session =

ConnectionFactory.getOrInitSession();

Student _student = null;

try {

//

Query query =

session.createQuery(

" from Student a where a.login =:login ");

query.setParameter("login", login);

/* . , */

_student = (Student) query.uniqueResult();

} catch (HibernateException e) {

e.printStackTrace();

ConnectionFactory.destroy();

} finally {

if (session!= null) {

try {

session.close();

} catch (HibernateException e) {

e.printStackTrace();

ConnectionFactory.destroy();

}

}

}

if (_student!= null) {

return new Boolean(true);

} else {

return null;

}

}

public static Student getStudent(String login,

String password) {

Session session =

ConnectionFactory.getOrInitSession();

Student _student = null;

try {

Query query =

session.createQuery(

"from Student a where a.login =:login");

query.setParameter("login", login);

_student = (Student) query.uniqueResult();

} catch (HibernateException e) {

e.printStackTrace();

ConnectionFactory.destroy();

} finally {

if (session!= null) {

try {

session.close();

} catch (HibernateException e) {

e.printStackTrace();

ConnectionFactory.destroy();

}

}

}

if (_student!= null) {

if (password.equals(_student.getPassword())) {

return _student;

} else

return null;

} else

return null;

}

//

// id mapping

public static void updateStudent(Student _student) {

Session session =

ConnectionFactory.getOrInitSession();

try {

Transaction tx = null;

tx = session.beginTransaction();

session.update(_student);

tx.commit();

} catch (HibernateException e) {

e.printStackTrace();

tx.rollback();

ConnectionFactory.destroy();

} finally {

if (session!= null) {

try {

session.close();

} catch (Exception e) {

e.printStackTrace();

ConnectionFactory.destroy();

}

}

}

}

//

// native mapping

public static Boolean addStudent(Student _student) {

Session session =

ConnectionFactory.getOrInitSession();

try {

Transaction tx = null;

tx = session.beginTransaction();

session.save(_student);

tx.commit();

} catch (HibernateException e) {

e.printStackTrace();

tx.rollback();

ConnectionFactory.destroy();

return null;

} finally {

if (session!= null) {

try {

session.close();

} catch (Exception e) {

e.printStackTrace();

ConnectionFactory.destroy();

return null;

}

}

}

return new Boolean(true);

}

//

public static void deleteStudent(Student _student) {

Session session =

ConnectionFactory.getOrInitSession();

try {

Transaction tx = null;

tx = session.beginTransaction();

session.delete(_student);

tx.commit();

} catch (HibernateException e) {

e.printStackTrace();

tx.rollback();

ConnectionFactory.destroy();

} finally {

if (session!= null) {

try {

session.close();

} catch (Exception e) {

e.printStackTrace();

ConnectionFactory.destroy();

}

}

}

}

}

 

SessionFactory , . ,
, SessionFactory, , , .

Session :

Session , Session per request .

3.1, Hibernate hibernate.current_session_context_class, , " jta ", " thread " "managed",
, org.hibernate.context.CurrentSessionContext / .

, SessionFactory.getCurrentSession(), , .

<property name= "current_session_context_class">thread </property> deleteStudent() .

public static void deleteStudent(Student _student) {

 

Session session =

ConnectionFatory.sessionFactory

.getCurrentSession();

try {

Transaction tx = null;

tx = session.beginTransaction();

session.delete(_student);

tx.commit();

} catch (HibernateException e) {

e.printStackTrace();

tx.rollback();

}

}

 

org.hibernate.context.ThreadLocalSessionContext, , beginTransaction() ,
commit() rollback().

- Apache Ant

- Apache Ant, , :

+lib

<Hibernate and third-party libraries>

+src

<All source files and packages including hbm files>

hibernate.cfg.xml

build.xml

build.xml Ant. :

<project name="hibernate-tutorial" default="compile">

<property name="sourcedir" value="${basedir}/src"/>

<property name="targetdir" value="${basedir}/bin"/>

<property name="librarydir" value="${basedir}/lib"/>

<path id="libraries">

<fileset dir="${librarydir}">

<include name="*.jar"/>

</fileset>

</path>

<target name="clean">

<delete dir="${targetdir}"/>

<mkdir dir="${targetdir}"/>

</target>

<target name="compile" depends=

"clean, copy-resources">

<javac srcdir="${sourcedir}"

destdir="${targetdir}"

classpathref="libraries"/>

</target>

<target name="copy-resources">

<copy todir="${targetdir}">

<fileset dir="${sourcedir}">

<exclude name="**/*.java"/>

</fileset>

</copy>

</target>

</project>

ant, . , build.xml :

<target name="run" depends="compile">

<java fork="true" classname="ClassName"

classpathref="libraries">

<classpath path="${targetdir}"/>

</java>

</target>

ClassName , ( main, ). , ant run.

 
6

STRUTS

Struts 2000 . . (Craig R. McClanahan) , Model/View/Controller. 2001 . Struts 1.0. Struts Jakarta, Apache Software Foundation. Jakarta Struts Web- Java Servlet and JSP.





:


: 2016-04-03; !; : 349 |


:

:

.
==> ...

1608 - | 1577 -


© 2015-2024 lektsii.org - -

: 0.05 .