Re-reported Hibernate entry example - www.hibernate.org.cn has an example, but it feels not very clear, mainly to see the primar

xiaoxiao2021-03-06  36

A few days ago, I learned Hibernate, using the plugin on the Eclipse, I saw some information on the Internet, I found www.hibernate.org.cn is good, but it is the new feature of Hibernate entry _2: one-to-one http://www.hibernate.org.cn/64.html) Not very detailed, trying to have no success for a long time, and the author did not design the database, I want to give an initiator: In fact One-to -one has a detailed description in the release of Hibernate, see 41 (Chinese version). According to DOC, there are in two ways to implement one to one. I have one of the demonstrations. (Using Many to One) Hehe, I feel that as long as one can be realized, it is generally not a problem, I am afraid that I will fail at the beginning, then there is no learning enthusiasm.

I. Database Design: - I use SQL Server Table 1 Person field Person_id key int email varchar (50) Name varchar (50) Table 2 Author field Author_id key int alias varchar (50) Person_id int5 KEY is very simple relationship, specific I will not say it in SQLServer. I don't say it.

II. ICLIPSE uses HibernateSyn and other tools. I don't talk about it. There are many information on this area. However, I have a question: I use HibernateSyn to generate the * .hbm file (which is mapping), if my database needs to be modified, I can only change the * .hbm file directly, no tool can directly update mapping, if you know Please tell me if you have this feature. Thanks!

3.Hibernate.cfg.xml file:

jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = test; SelectMethod = <- - local connection properties!> cursor com.microsoft.jdbc.sqlserver.SQLServerDriver sa scroll net.sf.hibernate.dialect.SQLServerDialect true true net.sf.hibernate.transaction.JTATransactionFactory java: comp / UserTransaction < mapping resource = "COM / TJL OG / PO / PERSON.HBM "/> Note: I put net.sf.hibernate.transaction.jtatransactionFactory java: comp / userTransaction Comment, because I am Use JDBC directly, if you don't comment, you will report an error!

Four .Person.hbm

5.author.hbm

< Generator class = "identity" /> 6. Test class CommONExample (actually using hibernateutil.java, only testing, if you need to use it, you need a DAO package) / * * Create Date 2005-1-24 * * / package com.tjlog.example;

/ ** * @Author administrator * @corpright dongyiping * / import.sf.hib.hibernate. *; Import.sf.hib.hibernate.cfg. *; Import java.util. *;

Import com.tjlog.po. *;

public class CommonExample {public CommonExample () {} public static void main (String [] args) {List list; try {Configuration cfg = new Configuration () configure ();. SessionFactory sessions = cfg.buildSessionFactory (); Session session = Sessions.openSession (); transaction tx = session.begintransaction (); Person Person = new person (); person.setname ("dongyiping"); person.seTemail (Yipingdong@tom.com); author author = new author () ;

Author.setalias ("pingping"); author.setPerson (Author);

Session.saveorUpdate (Person); tx.commit (); session.close ();} catch (Exception E) {system.out.println (e);}}} Insert success in my machine data!

IF u Have Anything About this test application, u can ask me by email: yip@tom.com.i am also the fresh man of hibernate.

IF u Have Anything About this test application, u can ask me by email: yip@tom.com.i am also the fresh man of hibernate.

3.Hibernate.cfg.xml file:

jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = test; SelectMethod = <- - local connection properties!> cursor com.microsoft.jdbc.sqlserver.SQLServerDriver sa scroll net.sf.hibernate.dialect.SQLServerDialect true true net.sf.hibernate.transaction.JTATransactionFactory java: comp / UserTransaction < mapping resource = "COM / TJL OG / PO / PERSON.HBM "/> Note: I put net.sf.hibernate.transaction.jtatransactionFactory java: comp / userTransaction Comment, because I am Use JDBC directly, if you don't comment, you will report an error!

Four .Person.hbm

5.author.hbm

< Generator class = "identity" /> 6. Test class CommONExample (actually using hibernateutil.java, only testing, if you need to use it, you need a DAO package) / * * Create Date 2005-1-24 * * / package com.tjlog.example;

/ ** * @Author administrator * @corpright dongyiping * / import.sf.hib.hibernate. *; Import.sf.hib.hibernate.cfg. *; Import java.util. *;

Import com.tjlog.po. *;

public class CommonExample {public CommonExample () {} public static void main (String [] args) {List list; try {Configuration cfg = new Configuration () configure ();. SessionFactory sessions = cfg.buildSessionFactory (); Session session = Sessions.openSession (); transaction tx = session.begintransaction (); Person Person = new person (); person.setname ("dongyiping"); person.seTemail (Yipingdong@tom.com); author author = new author () ;

Author.setalias ("pingping"); author.setPerson (Author);

Session.saveorUpdate (Person); tx.commit (); session.close ();} catch (Exception E) {system.out.println (e);}}} Insert success in my machine data!

IF u Have Anything About this test application, u can ask me by email: yip@tom.com.i am also the fresh man of hibernate.

IF u Have Anything About this Test Application, U can ask me by email: yipingdong@tom.com.

I am also the Fresh man of hibernate.

转载请注明原文地址:https://www.9cbs.com/read-64935.html

New Post(0)