Configure non-primary key Association in hibernate (one-to-many)

Source: Internet
Author: User

Table Structure:

Create Table primarytable (
Primarytableid int primary key,
Refkey varchar (16) -- associated key, note that it is not the ID
) Engine = InnoDB

Create Table childtable (
Childtableid int primary key,
Linkrefkey varchar (16) -- associate with the refkey in the primarytable table
) Engine = InnoDB

Hibernate ing file: (Note the red part)

Primarytable. 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>
<Hibernate-mapping>
<Class
Name = "primarytable"
Table = "primarytable">
<ID name = "ID" type = "Java. Lang. Integer">
<Column name = "primarytableid"/>
<Generator class = "assigned"> </generator>
</ID>

<! -- The refkey attribute must be written; otherwise, Hibernate will say that this attribute cannot be found. -->
<Property name = "refkey" type = "Java. Lang. String">
<Column name = "refkey" length = "16"/>
</Property>
<Set name = "childtables" inverse = "true">
<! -- <Key column = "linkrefkey foreign key property" property-ref = "refkey"/> -->
<Key column = "linkrefkey" property-ref = "refkey"/>
<One-to-define class = "childtable"/>
</Set>
</Class>
</Hibernate-mapping>

Childtable. 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>

<Hibernate-mapping>
<Class name = "childtable" table = "childtable">
<ID name = "ID" type = "Java. Lang. Integer">
<Column name = "childtableid"/>
<Generator class = "assigned"> </generator>
</ID>
<Property name = "linkrefkey" type = "Java. Lang. String">
<Column name = "linkrefkey" length = "16"/>
</Property>
</Class>
</Hibernate-mapping>

Http://topic.csdn.net/t/20060325/14/4639486.html

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.