Hibernate - 我如何持久化一个对象?

Hibernate - How do I persist an Object?

所以我制作了一个简单的休眠应用程序,并使用 HibernateUtil 静态方法启动了一个提供适当会话的 SessionFactory。

问题是 - 我如何才能坚持使用此代码?而且我对如何从这个设计中构建出来以合并 HibernateUtil 来满足我的每个对象需求感到更加困惑?

package com.hibernation.main;

import com.hibernation.model.Animal;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


/**
 * Created by jonathan on 27/12/16.
 */
public class Earth {

    public static void main(String[] args){

        Animal a = new Animal(1,"lizard", "gekko", "test");

        HibernateUtil();
    }

    public static void HibernateUtil(){

        // create configuration instance and pass in the
        // hibernate configuration file.
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");

        // version 4.x and up, service registry is being used.
        // The ServiceRegistry scopes the Service.
        // The ServiceRegistry manages the lifecycle of the Service.
        // The ServiceRegistry handles injecting dependencies into the Service
        // (actually both a pull and a push/injection approach are supported).
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        // create a Session factory instance: session factory creates sessions
        // at the request of clients.
        // conceptually, this is a single data store that is thread safe.
        // should be wrapped in a singleton (HibernateUtil being a common convention)
        // the internal state is immutable - once it is created the state is set.
        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);

        // get the current session.
        Session session = factory.getCurrentSession();

        // begin transaction
        session.getTransaction().begin();

        // Print out all required information
        System.out.println("Session Is Opened :: "+ session.isOpen());
        System.out.println("Session Is Connected :: "+ session.isConnected());



        // commit transaction
        session.getTransaction().commit();




    }



}

Question is - how can I persist using this code ?

不可以,必须修改代码

您必须像这样保存实体:

/**
 * Created by jonathan on 27/12/16.
 */
public class Earth {

    public static void main(String[] args){

        Animal a = new Animal(1,"lizard", "gekko", "test");

        HibernateUtil(a);
    }

    public static void HibernateUtil(Animal a){

        // create configuration instance and pass in the
        // hibernate configuration file.
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");

        // version 4.x and up, service registry is being used.
        // The ServiceRegistry scopes the Service.
        // The ServiceRegistry manages the lifecycle of the Service.
        // The ServiceRegistry handles injecting dependencies into the Service
        // (actually both a pull and a push/injection approach are supported).
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        // create a Session factory instance: session factory creates sessions
        // at the request of clients.
        // conceptually, this is a single data store that is thread safe.
        // should be wrapped in a singleton (HibernateUtil being a common convention)
        // the internal state is immutable - once it is created the state is set.
        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);

        // get the current session.
        Session session = factory.getCurrentSession();

        // begin transaction
        session.getTransaction().begin();

        // Print out all required information
        System.out.println("Session Is Opened :: "+ session.isOpen());
        System.out.println("Session Is Connected :: "+ session.isConnected());
        session.save(a);


        // commit transaction
        session.getTransaction().commit();




    }



}

当心

这是一个糟糕的例子,因为它是 very procedual and not Object-Oriented and containing only whats the minimum changes to your Code. There are many other problems you will have to fix like the problem that you will loose access to the constructed sessionfactory, please read about OOD

IoC 和 Demeter 定律迫使我们使用 TransactionManager。 Spring-TX 是通常最先进的实现。