Hibernate HelloWorld Annotation

3806 단어 annotation
Annontation 버 전 HelloWorld
대략적인 절차:
1. 건축 표
2. 실체 클래스 를 만 들 고 주석 을 추가 합 니 다.
3. hibenate 설정 파일, 실체 맵 설정 추가 에 주의 하 십시오.
4. 테스트
구체 적 인 절차 와 코드 는 다음 과 같다.
1. 건축 표
create table t_teacher(
id int primary key,
name varchar(20),
title varchar(20));
2. 실체 클래스 구축

package helloworld_annotation;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_teacher")
public class Teacher {

	private int id;
	private String name;
	private String title;
	
	
	@Id
	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;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

}


3. hibenate 프로필

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="connection.url">
		jdbc:oracle:thin:@localhost:1521:orcl
	</property>
	<property name="connection.username">tysp</property>
	<property name="connection.password">12345678</property>
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="dialect">
		org.hibernate.dialect.OracleDialect
	</property>
	
	<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

     <!-- Echo all executed SQL to stdout -->
     <property name="show_sql">true</property>

     <!-- Drop and re-create the database schema on startup -->
     <!-- <property name="hbm2ddl.auto">create</property> -->

     <!--       xml resource、     annotation class、     --
     <mapping class="helloworld_annotation.Teacher"/>

</session-factory>

</hibernate-configuration>

4. 테스트

package helloworld_annotation;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class TestTeacher {
	
	public static void main(String[] args){
		
		Teacher t = new Teacher();
		t.setId(1);
		t.setName("t1");
		t.setTitle("  ");
		
		Configuration conf = new AnnotationConfiguration();
		SessionFactory sf = conf.configure().buildSessionFactory();
		Session session = sf.openSession();
		Transaction tx = session.beginTransaction();
		tx.begin();
		session.save(t);
		tx.commit();
		session.close();
	}

}


좋은 웹페이지 즐겨찾기