Java

Hibernate学习笔记

2019-05-03  本文已影响2人  団长大人

Hibernate开发流程

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-osgi</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-proxool</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-infinispan</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
</dependencies>
public class Application {
    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        AccUser user = new AccUser();
        user.setUserName("hibernate");
        user.setPassword("1233333");
        user.setCreateTime(System.currentTimeMillis());
        session.save(user);
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}

Hibernate主键生成策略

<class name="com.libi.entity.AccUser" table="acc_user">
    <!--对象标识OID-->
    <id name="id" column="id">
        <generator class="具体策略"/>
    </id>
</class>

Hibernate的自动模式(生成映射文件和实体类)

HQL

public List<AccUser> selectAllUser() {
    Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
    SessionFactory sessionFactory = configuration.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Query<AccUser> query = session.createQuery("select au from AccUser au", AccUser.class);
    query.setFirstResult(0);
    query.setMaxResults(10);
    return query.getResultList();
}

解决传递当前Session问题

级联查询

延迟检索

实体类的状态

缓存

上一篇下一篇

猜你喜欢

热点阅读