使用annotations注解的hibernate的简单示例
来源:互联网 发布:js设置display为block 编辑:程序博客网 时间:2024/06/17 00:39
一、写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类似)
- 使用annotations注解的hibernate的简单示例
- hibernate的简单注解/使用
- 注解的简单示例
- Android主流IOC框架浅析(注解反射,Annotations,ButterKnife的简单使用)
- Hibernate的简单示例
- Hibernate Annotations 注解
- Hibernate Annotations 中英文注解
- 注解的Struts2简单示例
- 使用ehcache-spring-annotations开启ehcache的注解功能
- 黑马程序员---Java注解Annotations的详解以及使用实例
- 使用ehcache-spring-annotations开启ehcache的注解功能
- Android开发之注解框架的使用(android annotations)
- Android中注解(Support Annotations)的使用
- hibernate 注解的使用
- Hibernate注解的使用
- 使用Hibernate Annotations 维护多对多关系的心得
- 使用Hibernate Annotations 维护多对多关系的心得
- 使用Hibernate Annotations 维护多对多关系的心得
- 开发AJAX Enabled WebPart
- UAProf
- 破解一种光盘加密技术
- backdoor
- 托管注入
- 使用annotations注解的hibernate的简单示例
- Delphi多线程编程之同步读写全局数据
- oracle merge into 的用法详解+实例
- 第一日
- Oracle里用delete或者drop命令删除的数据或者表可以通过闪回功能找回数据
- flex端发送字符串到服务器端生成文件的例子(flex中得到request对象的方法)
- sqlLog enterprise快捷键
- 身份证号码产生规律
- 契子