使用annotations註解的hibernate的簡單樣本

來源:互聯網
上載者:User

一、寫bean實體類Book.java,並註解

1. package edu.hzu.entity;

 

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table (name = "book")
public class Book {
 private int id;
 private String name;
 private Author author;
 private Press press;
 private Date publishdate;
 private int pageCount;
 private float price;
 private String introduction;
 private String cover;
 private BookCategory bookCategory;
 
 @Id
 @GeneratedValue (strategy = GenerationType.AUTO)
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 
 @Column (name = "name")
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @ManyToOne (targetEntity = edu.hzu.entity.Author.class)
 public Author getAuthor() {
  return author;
 }
 public void setAuthor(Author author) {
  this.author = author;
 }
 
 @ManyToOne (targetEntity = edu.hzu.entity.Press.class)
 public Press getPress() {
  return press;
 }
 public void setPress(Press press) {
  this.press = press;
 }
 
 @Column (name = "date")
 public Date getPublishdate() {
  return publishdate;
 }
 public void setPublishdate(Date publishdate) {
  this.publishdate = publishdate;
 }
 
 @Column (name = "pageCount")
 public int getPageCount() {
  return pageCount;
 }
 public void setPageCount(int pageCount) {
  this.pageCount = pageCount;
 }
 
 @Column (name = "price")
 public float getPrice() {
  return price;
 }
 public void setPrice(float price) {
  this.price = price;
 }
 
 @Column (name = "introduction")
 public String getIntroduction() {
  return introduction;
 }
 public void setIntroduction(String introduction) {
  this.introduction = introduction;
 }
 
 @Column (name = "cover")
 public String getCover() {
  return cover;
 }
 public void setCover(String cover) {
  this.cover = cover;
 }
 
 @ManyToOne (targetEntity = edu.hzu.entity.BookCategory.class)
 public BookCategory getBookCategory() {
  return bookCategory;
 }
 public void setBookCategory(BookCategory bookCategory) {
  this.bookCategory = bookCategory;
 }
 
 

}

 

二、在src根目錄下見hibernate.cfg.xml檔案

<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</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>

        <mapping class="edu.hzu.entity.Book"/>
     <mapping class="edu.hzu.entity.BookCategory"/>
     <mapping class="edu.hzu.entity.Author"/>
     <mapping class="edu.hzu.entity.AuthorCategory"/>
     <mapping class="edu.hzu.entity.Site"/>
     <mapping class="edu.hzu.entity.Price"/>
     <mapping class="edu.hzu.entity.Press"/>

    </session-factory>

</hibernate-configuration>

 

三、做測試類別Test.java

 

package edu.hzu.test;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

 

public class Test {
 public static void main(String[] args) {
  AnnotationConfiguration a = new AnnotationConfiguration();
  Session session = a.configure().buildSessionFactory().openSession();
 }
}

 

通過此測試類別即可在資料庫中產生資料庫及表(當然這隻是一個例子,其實只給出了一個hibernate使用的架構,要真間資料庫,還得在對應的建相關的類:Author、BookCategory、Press等,方法與Book類似)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.