How to configure sleep mode in a standalone (Swing) application in eclipse?

I usually use hibernate with spring in web applications, so I use DI and maven to configure, now I want to use hibernate in a desktop application / swing that does not use maven or spring, and I was wondering the following:

  • Which banks do I need?
  • How to configure sleep mode and how to make a sample request?

please advise, thanks.

+5
source share
3 answers

I don't know about Spring, so you have to update your answer a bit if you really want to use it (for example, Spring has its own mechanism for initializing the entity manager).

Dependencies

, ( , , ), , Hibernate JPA (.. EntityManager):

org.hibernate:hibernate:3.2.7.ga
org.hibernate:hibernate-annotations:3.4.0.GA
org.hibernate:hibernate-entitymanager:3.4.0.GA

:

commons-collections:commons-collections:3.2.1
asm:asm:3.2
cglib:cglib:2.2
dom4j:dom4j:1.6.1
antlr:antlr:2.7.7
c3p0:c3p0:0.9.1.2

maven central.

persistence.xml META-INF:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NONJTAPersistenceUnit" transaction-type="RESOURCE_LOCAL">

        <class>com.yourpackage.EntityClass1</class>
        <class>com.yourpackage.EntityClass2</class>
        <class>com.yourpackage.EntityClass3</class>

        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://hostXYZ/yourdatabase"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

/ .

Create Entity EntityClass1, EntityClass2, EntityClass3, persitence.xml .

EntityManager..., EE, EntityManagerFactoty:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("NONJTAPersistenceUnit");
EntityManager em = emf.createEntityManager();

( , Spring , ).

, , persist, :

em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();

, Spring.

EDIT

:

Query query = em.createQuery("select e from EntityClass1 where e.name = :name");
query.setParameter(:name, "foo");
List results = query.getResultList();

EDIT

:

hibernate-core-3.5.1-Final
hibernate-entitymanager-3.5.1-Final
hibernate-jpa-2.0-api-1.0.0.Final
hibernate-annotations-3.5.1-Final
hibernate-commons-annotations-3.2.0.Final
dom4j-1.6.1
slf4j-api-1.6.4
slf4j-log4j12-1.6.4
...
+6

WebApp StandAlone, Spring/Hibernate . JAR . spring JAR, Web, .

JAR "/lib" , .mf Maven.

Java , / ApplciationContext, , ...

public static void main(String[] args) {
...

String[] contextXml = new String[]{ "resources/spring-context.xml", "resources/spring-db.xml" };

ApplicationContext ctx = new ClassPathXmlApplicationContext(contextXml);

// invoke your business logic

MyBusinessService bean = getBean(MyBusinessService.class);

bean.doSomething();
...
}

DAO

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class MyDaoHibernate extends HibernateDaoSupport {


    /*** Functional methods ***/

    public void save(MyValueObject vo) throws DatabaseException {       
        getHibernateTemplate().saveOrUpdate(vo);
    }

    public MyValueObject get(Long id) throws DatabaseException {
        return getHibernateTemplate().get(MyValueObject.class, id);
    }

    /*** Getter & Setters ***/

    @Autowired
    public void initSessionFactory(SessionFactory sessionFactory) {
        this.setSessionFactory(sessionFactory);
    }   
}

[EDIT]

, Yanflea, , , , -. commons-dbcp.jar spring...

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"    value="com.sybase.jdbc2.jdbc.SybDriver" />
    <property name="url"                value="${jdbc.url}" />
    <property name="username"           value="${jdbc.username}" />
    <property name="password"           value="${jdbc.password}" />
</bean> 
+1

SessionFactory - Hibernate ( - ). hibernate http://sourceforge.net/projects/hibernate/files/hibernate4/. lib. , . , SessionFactory. buildSessionFactory() Configuration ( , hibernate). hibernate.cfg.xml( ). http://javabrains.koushik.org/ . There, in the Hibernate section, just browse through the first few guides. There he explained very well.

0
source