hibernate入门

Posted Danial7777777

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate入门相关的知识,希望对你有一定的参考价值。

package com.hello;

public class Account {
    
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
package com.hello;

import java.util.HashSet;
import java.util.Set;

public class User {
    private int id;
    private String name;
    private String desc1;
    private byte[] photo;
    
    private Set<Account> accounts=new HashSet<Account>();
    
    private Set<String> addresses=new HashSet<String>();    
    
    public Set<String> getAddresses() {
        return addresses;
    }
    public void setAddresses(Set<String> addresses) {
        this.addresses = addresses;
    }
    public Set<Account> getAccounts() {
        return accounts;
    }
    public void setAccounts(Set<Account> accounts) {
        this.accounts = accounts;
    }
    public byte[] getPhoto() {
        return photo;
    }
    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
    public String getDesc1() {
        return desc1;
    }
    public void setDesc1(String desc) {
        this.desc1 = desc;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hello">
    <!-- 
        name属性:哪个类
        table属性:对应哪个表,如果不写,默认的表名就是类的简单名称
    -->
    <class name="Account" table="t_account">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"  not-null="true"/> 
    </class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hello">
    <!-- name属性:哪个类 table属性:对应哪个表,如果不写,默认的表名就是类的简单名称 -->
    <class name="User" table="t_user">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name" not-null="true" />
        <property name="desc1" column="desc1" />
        <property name="photo" column="photo" length="102400" />
        <set name="accounts" inverse="true">
            <key column="uid"></key>
            <one-to-many class="com.hello.Account" />
        </set>
        <!-- sort="natural" -->
        <set name="addresses" table="user_address"  order-by="address DESC">
            <key column="uid"></key>
            <element type="string" column="address"></element>
        </set>
    </class>
</hibernate-mapping>
package com.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.EnumSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import com.hello.Account;
import com.hello.User;

public class HelloTest {

    private static SessionFactory sessionFactory;

    static {
        sessionFactory = new Configuration()
        .configure() // 读取配置文件
        .buildSessionFactory();
    }

    // 根据配置生成表结构
    @Test
    public void test() {
        //hibernate 3.0版本的
        /*  Configuration cfg = new Configuration().configure();
        SchemaExport schemaExport = new SchemaExport(cfg);

        // 第一个参数script的作用: print the DDL to the console
        // 第二个参数export的作用: export the script to the database
        schemaExport.create(true, true);*/

        //hibernate 5.0
        Configuration conf = new Configuration().configure();//1、读取配置文件
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
    }

    @Test
    public void hello(){
        byte[] photo = null ;
        try {
            InputStream in=new FileInputStream("C://Users//Administrator//Desktop//1.jpg");
            photo = new byte[in.available()];
            in.read(photo);
            in.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("图片导入流失败!");
        }


        Session session=sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        User user=new User();
        user.setId(1);
        user.setName("a");
        user.setPhoto(photo);
        Account account=new Account();
        account.setId(1);
        account.setName("1");
    user.getAccounts().add(account);
        //    user.getAddresses().add("天安门");
        //    user.getAddresses().add("故宫");
        session.save(account);
        session.save(user);
        
        tx.commit();
        
        Session session2=sessionFactory.openSession();
        Transaction tx2= session2.beginTransaction();
        User user2 = session.get(User.class, 3);
        System.out.println(user2.getAccounts());
        tx2.commit();
    }


    @Test
    public void testGet(){

        Session session=sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        User user = session.get(User.class, 1);
        System.out.println(user.getName());
        System.out.println(user.getAddresses());
    }
}

 

以上是关于hibernate入门的主要内容,如果未能解决你的问题,请参考以下文章

hibernate多对多 一对多 及简单入门

hibernate框架的简单入门

Hibernate简述及入门实例

推荐net开发cad入门阅读代码片段

Hibernate入门

Hibernate入门