- 浏览: 138307 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
renovatio:
...
JAVA的远程控制系统(远程监控)实现 -
xiaofan_0204:
有点恶心呀,少了一个类……………………
JAVA中HTTP的多线程下载程序 -
h286271819:
你奶奶,DownloadNetThread 类呢?
JAVA中HTTP的多线程下载程序 -
ileson:
LINUX 之旅 (-)起因 -
panshunchang:
import java.rmi.*;
public clas ...
java 分布式RMI的简单使用
作者:shazi.
数据库的生成由配置文件自动生成。例子中两个实体类,学生类和教师类。一个学生可以由多个教师,一个教师也可以有多个学生。
主要代码如下:
(一)配置文件:hibernate-cfg.xml
xml 代码
- <?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="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="connection.url">
- jdbc:mysql://localhost:3306/good
- </property>
- <property name="connection.username">root</property>
- <property name="connection.password">admin</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="myeclipse.connection.profile">
- database
- </property>
- <property name="show_sql">true</property>
- <property name="jdbc.fetch_size">50</property>
- <property name="jdbc.batch_size">25</property>
- <property name="hbm2ddl.auto">true</property>
- <mapping resource="mapping/student.hbm.xml" />
- <mapping resource="mapping/teacher.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
(二)日志文件:log4j.xml
xml 代码
- ### direct log messages to stdout ###
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
- ### direct messages to file hibernate.log ###
- #log4j.appender.file=org.apache.log4j.FileAppender
- #log4j.appender.file.File=hibernate.log
- #log4j.appender.file.layout=org.apache.log4j.PatternLayout
- #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
- ### set log levels - for more verbose logging change 'info' to 'debug' ###
- log4j.rootLogger=info, stdout
- #log4j.logger.org.hibernate=info
- log4j.logger.org.hibernate=info
- ### log HQL query parser activity
- #log4j.logger.org.hibernate.hql.ast.AST=debug
- ### log just the SQL
- #log4j.logger.org.hibernate.SQL=debug
- ### log JDBC bind parameters ###
- log4j.logger.org.hibernate.type=info
- #log4j.logger.org.hibernate.type=debug
- ### log schema export/update ###
- log4j.logger.org.hibernate.tool.hbm2ddl=debug
- ### log HQL parse trees
- #log4j.logger.org.hibernate.hql=debug
- ### log cache activity ###
- #log4j.logger.org.hibernate.cache=debug
- ### log transaction activity
- #log4j.logger.org.hibernate.transaction=debug
- ### log JDBC resource acquisition
- #log4j.logger.org.hibernate.jdbc=debug
- ### enable the following line if you want to track down connection ###
- ### leakages when using DriverManagerConnectionProvider ###
- #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
(三)HibernateSessionFactory.java文件,主要是SessionFactory的创建和Session的创建。
java 代码
- package dao;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.cfg.Configuration;
- /**
- * Configures and provides access to Hibernate sessions, tied to the
- * current thread of execution. Follows the Thread Local Session
- * pattern, see {@link http://hibernate.org/42.html }.
- */
- public class HibernateSessionFactory {
- /**
- * Location of hibernate.cfg.xml file.
- * Location should be on the classpath as Hibernate uses
- * #resourceAsStream style lookup for its configuration file.
- * The default classpath location of the hibernate config file is
- * in the default package. Use #setConfigFile() to update
- * the location of the configuration file for the current session.
- */
- private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
- private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
- private static Configuration configuration = new Configuration();
- private static org.hibernate.SessionFactory sessionFactory;
- private static String configFile = CONFIG_FILE_LOCATION;
- private HibernateSessionFactory() {
- }
- /**
- * Returns the ThreadLocal Session instance. Lazy initialize
- * the <code>SessionFactory</code> if needed.
- *
- * @return Session
- * @throws HibernateException
- */
- public static Session getSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- if (session == null || !session.isOpen()) {
- if (sessionFactory == null) {
- rebuildSessionFactory();
- }
- session = (sessionFactory != null) ? sessionFactory.openSession()
- : null;
- threadLocal.set(session);
- }
- return session;
- }
- /**
- * Rebuild hibernate session factory
- *
- */
- public static void rebuildSessionFactory() {
- try {
- configuration.configure(configFile);
- sessionFactory = configuration.buildSessionFactory();
- } catch (Exception e) {
- System.err
- .println("%%%% Error Creating SessionFactory %%%%");
- e.printStackTrace();
- }
- }
- /**
- * Close the single hibernate session instance.
- *
- * @throws HibernateException
- */
- public static void closeSession() throws HibernateException {
- Session session = (Session) threadLocal.get();
- threadLocal.set(null);
- if (session != null) {
- session.close();
- }
- }
- /**
- * return session factory
- *
- */
- public static org.hibernate.SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- /**
- * return session factory
- *
- * session factory will be rebuilded in the next call
- */
- public static void setConfigFile(String configFile) {
- HibernateSessionFactory.configFile = configFile;
- sessionFactory = null;
- }
- /**
- * return hibernate configuration
- *
- */
- public static Configuration getConfiguration() {
- return configuration;
- }
- }
(四) 创建Student.java,POJO类。
java 代码
- package mapping;
- import java.util.HashSet;
- import java.util.Set;
- public class Student implements java.io.Serializable{
- private Integer id;
- private String name;
- private Set teacher=new HashSet();
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public Set getTeacher() {
- return teacher;
- }
- public void setTeacher(Set teacher) {
- this.teacher = teacher;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
(五)student.hbm.xml
xml 代码
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse - Hibernate Tools
- -->
- <hibernate-mapping>
- <class name="mapping.Student" table="student">
- <id name="id" type="integer">
- <column name="ID" />
- <generator class="identity"></generator>
- </id>
- <property name="name" type="string">
- <column name="name" length="100" />
- </property>
- <set name="teacher" inverse="true" cascade="save-update" table="student_teacher">
- <key column="s_id" not-null="false">
- </key>
- <many-to-many class="mapping.Teacher" column="t_id" outer-join="true" />
- </set>
- </class>
- </hibernate-mapping>
(六)Teacher.java
java 代码
- package mapping;
- import java.util.HashSet;
- import java.util.Set;
- /**
- * Teacher generated by MyEclipse - Hibernate Tools
- */
- public class Teacher extends mapping.Student implements java.io.Serializable {
- // Fields
- private Integer id;
- private String name;
- private Set students = new HashSet(0);
- // Constructors
- /** default constructor */
- public Teacher() {
- }
- /** full constructor */
- public Teacher(String name, Set students) {
- this.name = name;
- this.students = students;
- }
- // Property accessors
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set getStudents() {
- return this.students;
- }
- public void setStudents(Set students) {
- this.students = students;
- }
- }
(七)teacher.bhm.xml
xml 代码
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse - Hibernate Tools
- -->
- <hibernate-mapping>
- <class name="mapping.Teacher" table="teacher">
- <id name="id" type="integer">
- <column name="id" />
- <generator class="native"></generator>
- </id>
- <property name="name" type="string">
- <column name="name" length="100" />
- </property>
- <set name="students" cascade="save-update" table="student_teacher">
- <key column="t_id" not-null="false">
- </key>
- <many-to-many class="mapping.Student" column="s_id" outer-join="true"/>
- </set>
- </class>
- </hibernate-mapping>
(八)测试类:Test.java,使用SchemaExport schemaExport = new SchemaExport(config);生成数据库代码。
java 代码
- package dao;
- import mapping.Student;
- import mapping.Teacher;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- org.hibernate.cfg.Configuration config=new org.hibernate.cfg.Configuration().configure();
- System.out.println("Creating tables...");
- SchemaExport schemaExport = new SchemaExport(config);
- schemaExport.create(true, true);
- System.out.println("Table created.");
- Student s = new Student();
- s.setName("rose");
- Teacher t = new Teacher();
- t.setName("admin");
- s.getTeacher().add(t);
- t.getStudents().add(s);
- Session session = HibernateSessionFactory.getSession();
- Transaction tx = session.beginTransaction();
- session.save(t);
- session.save(s);
- tx.commit();
- session.close();
- }
- }
运行即可:
- manytomany.rar (38.9 KB)
- 下载次数: 40
发表评论
-
build.xml(xdoclet,hibernate,SchemaExportTask)
2007-08-04 16:49 2017xml 代码 xml version=" ... -
Hibernate 的Xdoclet模板
2007-07-21 08:23 1804xml 代码 <?xml version= ... -
Hibernate配置文件--hibernate.cfg.xml
2007-07-19 09:48 2930Hibernate的mysql配置。 xml 代码 ... -
Hibernate的Session管理之HibernateSessionFactory
2007-07-19 09:41 4714myeclipse 生成的hibernateUtil类,感觉实 ... -
三种整合 Struts 应用程序与 Spring 的方式
2007-07-14 17:04 1509URL:http://www.ibm.com/develo ... -
jspSmartUpload 实现jsp文件上传下载
2007-07-10 09:51 5342转载 www.csdn.net ... -
正则表达式收集
2007-03-20 14:36 752正则表达式收集 只能输入数字:"^[0-9]*$& ... -
经典正则表达式
2007-03-20 14:38 664转载 www.qysoft.net 正 ...
相关推荐
Hibernate映射一对多关联关系
hibernate实现多对多关联关系源码
这里包含了hibernate多对一单向关联关系实现源码,希望对你有用。
hibernate(多对多关系映射)实现存储,内附sql脚本和详细的注释,适合初学者
hibernate多对多的关系实例hibernate多对多的关系实例hibernate多对多的关系实例
Hibernate多对多关联关系demo
Hibernate一对一,一对多,多对多实例
hibernate多对多关系映射案例,配有对应的jar包。
hibernate 映射关系学习入门 多对多实体映射 源码
hibernate多对多映射关系,操作实体类,第三篇,使用于新手
Hibernate 多对一 一对多 操作实例
Hibernate多对多关系
hibernate一对一的关系hibernate一对一的关系hibernate一对一的关系hibernate一对一的关系hibernate一对一的关系hibernate一对一的关系
hibernate 多对多 映射 翻转 hibernate多对多关系项目 自己做的hibernate项目 上传上来和大家分享
hibernate 关系注解:Hibernate Annotation关系映射
Hibernate 多表映射关系配置 如,一对一 多对一 多对多等
在hibernate中有多种多样的对应关系,在这里我们具体写出一对一,多对多,一对多的对应关系代码以及数据库,希望对大家有
hibernate多对多关系的增删查改 修改有问题
后面的hibernate代码,我会慢慢的上传上去,这些资源都是免费的,希望可以帮到大家。