Hibernate MTM Federated Primary Key

Source: Internet
Author: User
Tags set set

//apply to Table no other columns, only primary key//course.java entity class PackageCom.tao.pojo;ImportJava.util.HashSet;ImportJava.util.Set; Public classCourse {Private intID; PrivateString name; PrivateSet<student> stu=NewHashset<student>();  PublicCourse () {Super(); }     PublicCourse (intID, String name) {        Super();  This. ID =ID;  This. Name =name; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicSet<student>Getstu () {returnStu; }     Public voidSetstu (set<student>Stu) {         This. Stu =Stu; } @Override PublicString toString () {return"Course [id=" + ID + ", name=" + name + "]"; }}//student.java entity class PackageCom.tao.pojo;ImportJava.util.HashSet;ImportJava.util.Set; Public classStudent {Private intID; PrivateString name; PrivateString Gender; PrivateSet<course> cou=NewHashset<course>();  PublicStudent () {Super(); }     PublicStudent (intID, string name, String gender) {        Super();  This. ID =ID;  This. Name =name;  This. Gender =gender; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getgender () {returngender; }     Public voidSetgender (String gender) { This. Gender =gender; }     PublicSet<course>Getcou () {returncou; }     Public voidSetcou (set<course>cou) {         This. Cou =cou; } @Override PublicString toString () {return"Student [id=" + ID + ", name=" + name + ", gender=" + Gender + "]"; }}//profile//course.hbm.xml file name<?xml version= "1.0"? ><! DOCTYPE hibernate-Mapping Public"-//hibernate/hibernate Mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >< Hibernate-mapping> <className= "Com.tao.pojo.Course" table= "Course" > <id name= "id" > <!--auto-grow--<genera Torclass= "Native" ></generator> </id> <property name= "name"/> <!--Name: attribute name in Entity (Claz Z name) Clazz: attribute type (clazz type is clazz)-<!--column: Foreign key Column name--<!--Stu is the name of the set set in the entity class, SC is the table name, CID is course Foreign keys in SC table,--<set name= "Stu" table= "SC" > <key column= "cid" ></key> <! The object type student class SID that is stored in the--set collection is the student foreign key in the SC table--and <many-to-manyclass= "Com.tao.pojo.Student" column= "Sid" ></many-to-many> </set> </class>//Student.hbm.xml<?xml version= "1.0"? ><! DOCTYPE hibernate-Mapping Public"-//hibernate/hibernate Mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >< Hibernate-mapping> <className= "Com.tao.pojo.Student" table= "Student" > <id name= "id" > <!--auto-grow--<gene Ratorclass= "Native" ></generator> </id> <!--normal column--<property name= "name"/> & Lt;property name= "Gender"/> <!--the configuration SID of the set set is student column name in the SC table---<set name= "cou" table= "SC"        ; <!--Student own key--<key column= "Sid" ></key> <!--Set Database Type CID is course in SC table Foreign keys--<!--course--<many-to-manyclass= "Com.tao.pojo.Course" column= "CID" ></many-to-many> </set> </class>//Map file Hibernate.cfg.xml
<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-Configuration Public"-//hibernate/hibernate Configuration DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" & Gt;//localhost:3306/test0105_mtm</property><property name= "Connection.username" >root</property> <property name= "Connection.password" >root& lt;/property> <property name= "Show_sql" >true</property> <property name= "Format_sql" >true</property> <!--automatically create or update a table (without creating it, update it)-<property name= "Hbm2ddl.auto" >update</propert y> <!--database dialect (set the database dialect if the foreign key is not generated)-<!--Org.hibernate.dialect.MySQL5InnoDBDialect is Mys The class name of the QL database dialect in the Java file MySQL5 Mysql5innodbdialect-<!--If the execution is finished without an error, the foreign key or not, the table was deleted, and then executed again- <property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</property> <!--configuration map Pieces--<mapping resource= "Com/tao/pojo/course.hbm.xml"/> <mapping resource= "com/tao/pojo/student . Hbm.xml "/> </session-factory>//Test class//Testjunit.java Packagecom.tao.test;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;ImportCom.tao.pojo.Course;Importcom.tao.pojo.Student; Public classTestjunit {sessionfactory factory; Session session; @Before Public voidtest001 () {Configuration Configure=NewConfiguration (). Configure (); Factory=configure.buildsessionfactory (); Session=factory.opensession (); Session.begintransaction (); } @After Public voidtest002 () {session.gettransaction (). commit (); Session.close (); Factory.close (); } @Test Public voidteststudent () {}} The test class does not have to write content, here with the teststudent() test

Hibernate MTM Federated Primary Key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.