Hibernate one to one configuration details

2010-10-22  来源:本站原创  分类:Java  人气:131 

This is my personal time to learn habinate test case.
Established before using MyEclipse habinate (this is not to say, Oh, that is, to be with a good data source)

. Package Name: cn.hdu .***( entity; service; test; util)
First, write the two entity classes Address (private int add_id;
private String city; private String country; private Company company;) and the Company (private int c_id; private String c_name; private Address address;), generating get, set methods, and then configure the mapping entity class file Address.hbm.xml and Company . hbm.xml.
---------- Address. Hbm.xm
<? 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 ">
<class name="cn.hdu.entity.Address" table="ADDRESS">
<id name="add_id" column="ADD_ID">
<generator> <param

name = "sequence"> ADDRESS_SEQUENCES </ param> </ generator>
</ Id>
<property name="city" column="CITY"> </ property>
<property name="country" column="COUNTRY"> </ property>

<One-to-one name = "company" cascade = "save-update"

class = "cn.hdu.entity.Company" lazy = "false"> </ one-to-one>
</ Class>
</ Hibernate-mapping>

----------- Company. Hbm.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 ">
<class name="cn.hdu.entity.Company" table="COMPANY">
<id name="c_id" column="C_ID">
<param name="sequence"> COMPANY_SEQUENCES </ param> </ generator>
</ Id>
<property name="c_name" column="C_NAME"> </ property>
<Many-to-one name = "address" column = "ADD_ID"
class = "cn.hdu.entity.Address" unique = "true" lazy = "false"
cascade = "save-update"> </ many-to-one>
</ Class>
</ Hibernate-mapping>
NOTE: The above many-to-one, not to say that is many to one, this is only Company in the address attribute that points to Address the add_id property, ie one to one is an entity that may correspond to another entity in an attribute of a property.
COMPANY_SEQUENCES and ADDRESS_SEQUENCES representatives of the two sequences in the database,
Needs its own database and then create; id on behalf of the primary key, name = "" strictly equal to the entity class property name, column = "" representative of the database table field names, usually capitalized;
lazy = "false" said do not delay load, the main approach is to ensure that get the value from the database to avoid delay when the error occurred. cascade that when carrying out the operation at the same time habinate-one operation.

Do not forget to finish mapping the mapping file after the write hibernate.cfg.xml;
<property name="show_sql"> true </ property>
<mapping resource="cn/hdu/entity/Company.hbm.xml" />
<mapping resource="cn/hdu/entity/Address.hbm.xml" />
</ Session-factory>

One show_sql .. true; behalf of the Executive when the output sql statements.

Go after the test, write an implementation class
public class ServiceImp {
public void insertCompany (Company company) {

Session session = HibernateSessionFactory.getSession

Transaction tc = session.beginTransaction ();
session.save (company);
tc.commit ();
HibernateSessionFactory.closeSession ();

Test Class:
public class Test {
public static void main (String [] args) {
Service service = new ServiceImp ();
Company company = new Company ();
Address address = new Address ();
address.setCity ("beijin");
address.setCountry ("zhonguo");
company.setC_name ("CP");
company.setAddress (address);
service.insertCompany (company);
System.out.print ("OK ...");
Two tables in the database and then have a look, should be inserted into the data.
Oh, what I do not understand you can send mail ah, we learn from each other progress ~! ! ! !

Specific code in the annex src.

  • Hibernate one to one configuration details 2010-10-22

    This is my personal time to learn habinate test case. Established before using MyEclipse habinate (this is not to say, Oh, that is, to be with a good data source) . Package Name: cn.hdu .***( entity; service; test; util) First, write the two entity c

  • Mysql database server configuration file / etc / my.cnf configuration details 2010-10-24

    The following is a Mysql database server configuration file / etc / my.cnf configuration details. Applications is the InnoDB engine, 4-core CPU, 32 Bit SUSE. [Client] port = 3306 socket = / tmp / mysql.sock # The MySQL server [Mysqld] bind-address =

  • spring hibernate many data source configuration collection 2009-04-09

    Spring2.0.1 later many have supported the configuration data source, and can be run at a time when dynamic loading of different data sources. Through inheritance AbstractRoutingDataSource on many data sources can achieve the dynamic conversion. Does

  • TOMCAT6 + SPRING + HIBERNATE JNDI data source configuration 2009-05-01

    This article carried: http://blog.chinaunix.net/u2/63740/showart_515514.html 1, in the context of context.xml inside tag add the following code: <Resource name = "jdbc / mysql" auth = "Container" type = "javax.sql.DataSource&qu

  • hibernate (three) connection pool configuration mysql 2009-05-15

    I spent three nights of the time, the hibernate connection pool with the three commonly used configuration of the study, in the hope that everyone has to help. The following are three types of connections to connect MySQl as an example. <!-- JDBC Dri

  • iBatis configuration details 2010-07-24

    iBatis simple CRUD search operation is very easy to grasp, let's take a look at the detailed configuration iBatis. Although iBatis small flexible, but its scalability is also very strong. iBatis core configuration file is SqlMapConfig.xml, here we lo

  • hibernate graffiti (2) - annotation configuration 2010-11-15

    org.hibernate.annotations.common.reflection.java.JavaXPackage extends JavaXAnnotatedElemen: - JavaReflectionManager - AnnotatedElement - Public JavaXPackage (Package, JavaReflectionManager): Direct call super constructor with the parameters were assi

  • Hibernate annotation JPA based configuration (annotation configuration) 2010-12-03

    JPA (Java Persistence API), is an integral part of EJB3.0, it significantly simplifies the EJB persistence and provides an object-relational mapping method, which allows developers to declaratively define how to use a standard portable way the Java o

  • SVN configuration details 2011-01-28

    SVN version management tools notes: I. Configuration notes: ①: installation file: Server Software: VisualSVN-Server-2.1.1.msi Client software: TortoiseSVN- II. Tools essays: ①: Subversion is a fantastic hammer, but be c

  • Hibernate Object Relational Mapping Configuration 2010-03-29

    1, class innuendo <class name = "com.gdlinkway.hibernate. students" / / class name table = "student" / / database table name schema = "SCOTT" / / database user dynamic-update = "true" / / dynamically generate the

  • Spring Hibernate integration of annotation-based configuration 2010-03-27

    sessionFactory using org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean Other attributes as usual, and more on the OK to set a annotatedClasses Paste code: <bean > <property name="dataSource"> <ref bean=&qu

  • java hibernate in the inverse configuration 2010-04-02

    Today, the master table in dealing with business, I found my first save the child table of the main table taskbillid saved to the table to the child. But when I was on the main table when a save operation, the child table's foreign key id on to empty

  • FckEditor English tutorial manual configuration details 2010-04-12

    First of all, FCKEDITOR's performance is very good, just a little time users can load FCKEDITOR required documents. For other online editor, it is almost difficult to resolve the problem, because when opening the editor load too many files. such CUTE

  • HelixServer Unlimited 11 for Linux installation and configuration details (1) 2010-05-31

    Source: http://www.cublog.cn/u/10500/showart.php?id=118180 Online circulating one "<stream media detail Helix Server installation, configuration>> article gave a detailed Helix_DNA_Server_10.1.tar.gz installation method, and today I successf

  • OpenVPN configuration details 2010-06-09

    1, the server environment and purpose Server two network cards, a connection to the external network , A connection to the intranet External network IP: X X X X Office intranet IP: Objective: Through VPN access in the Internet environmen

  • JSP to connect SQL Server 2000 system configuration details 2010-07-14

    To SUN official site (http://java.sun.com) to download J2SDK installation file: j2sdk-1_4_2_04-windows-i586-p.exe, After the download is installed J2SDK; installed after setting the environment variable: My computer --- properties --- Advanced --- En

  • Log4j configuration details 2010-10-21

    First attach a frequently used example of a configuration. # Properties for configuring Log4j for the OnPoint Application (Dev Version) # This is the configuring for logging displayed in the Application Server # Log4j.rootCategory = info, stdout, mai

  • Windows XP IIS PHP5 configuration details 2010-11-01

    First, the software is ready: The following are the latest official version ended 2005-4-20 PHP (5.1.2): http://www.php.net MySQL (5.0.19): http://www.mysql.com Zend Optimizer (2.6.2): http://www.zend.com phpMyAdmin (2.8.0 .2): http://www.phpmyadmin.

  • svn integration process eclipse configuration details 2010-11-02

    Faces younger brother graduated recently with another friend for a project together, built himself a simple development environment (because the equipment is limited, only two laptops and a network cable), configured ip, a cable will form two noteboo

  • web.xml configuration details 2010-11-12

    1 defines the root element of the head and Deployment descriptor file like all XML files, an XML header must begin. The first statement can use the XML version and character encoding the file is given. DOCYTPE declaration must appear immediately afte