hibernate - how to properly generate .hbn.xml mapping files for a java class, which corresponds to a table with a composite primary key? -
in postgresql erp database, have table "custtable", has composite primary key, made following fields:
accountnum (string) partition (long) dataareaid (string)
as understand, because of composite key, have implement separat pojo, serve id field "custtable" hibernate class:
public class custtableid { private string accountnum; private long partition; private string dataareaid; public custtableid(){ } public string getaccountnum(){ return accountnum; } public void setaccountnum( string ) { accountnum = an; } public long getpartition(){ return partition; } public void setpartition(long part){ partition = part; }
then, can use id class in main class:
@entity public class custtable implements java.io.serializable { @id private custtableid custtableid;
having done that, when use eclipse hibernate tools menu generate .hbn.xml mapping file main class, following mapping:
<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- generated sep 7, 2016 10:15:45 hibernate tools 3.5.0.final --> <hibernate-mapping> <class name="com.myproject.custtable" table="custtable"> <id name="orgid" type="java.lang.string"> <column name="orgid" /> <generator class="assigned" /> </id> <many-to-one name="custtableid" class="com.uptake.symphony.data.custtableid" access="field" fetch="join"> <column name="custtableid" /> </many-to-one> <property name="invoiceaccount" type="java.lang.string"> <column name="invoiceaccount" /> </property> <property name="custgroup" type="java.lang.string"> <column name="custgroup" /> </property> <property name="linedisc" type="java.lang.string"> <column name="linedisc" /> </property> <property name="paymtermid" type="java.lang.string"> <column name="paymtermid" /> </property> <property name="cashdisc" type="java.lang.string"> <column name="cashdisc" /> </property> <property name="currency" type="java.lang.string"> <column name="currency" /> </property> <property name="intercompanyautocreateorders" type="int"> <column name="intercompanyautocreateorders" /> </property> <property name="salesgroup" type="java.lang.string"> <column name="salesgroup" /> </property> <property name="blocked" type="int"> <column name="blocked" /> </property> <property name="onetimecustomer" type="int"> <column name="onetimecustomer" /> </property> <property name="accountstatement" type="int"> <column name="accountstatement" /> </property> <property name="creditmax" type="java.math.bigdecimal"> <column name="creditmax" /> </property> <property name="mandatorycreditlimit" type="int"> <column name="mandatorycreditlimit" /> </property> <property name="vendaccount" type="java.lang.string"> <column name="vendaccount" /> </property> <property name="pricegroup" type="java.lang.string"> <column name="pricegroup" /> </property> <property name="multilinedisc" type="java.lang.string"> <column name="multilinedisc" /> </property> <property name="enddisc" type="java.lang.string"> <column name="enddisc" /> </property> <property name="vatnum" type="java.lang.string"> <column name="vatnum" /> </property> <property name="inventlocation" type="java.lang.string"> <column name="inventlocation" /> </property> <property name="dlvterm" type="java.lang.string"> <column name="dlvterm" /> </property> <property name="dlvmode" type="java.lang.string"> <column name="dlvmode" /> </property> <property name="markupgroup" type="java.lang.string"> <column name="markupgroup" /> </property> <property name="clearingperiod" type="java.lang.string"> <column name="clearingperiod" /> </property> <property name="freightzone" type="java.lang.string"> <column name="freightzone" /> </property> <property name="creditrating" type="java.lang.string"> <column name="creditrating" /> </property> <property name="taxgroup" type="java.lang.string"> <column name="taxgroup" /> </property> <property name="statisticsgroup" type="java.lang.string"> <column name="statisticsgroup" /> </property> <property name="paymmode" type="java.lang.string"> <column name="paymmode" /> </property> <property name="commissiongroup" type="java.lang.string"> <column name="commissiongroup" /> </property> <property name="bankaccount" type="java.lang.string"> <column name="bankaccount" /> </property> <property name="paymsched" type="java.lang.string"> <column name="paymsched" /> </property> <property name="contactpersonid" type="java.lang.string"> <column name="contactpersonid" /> </property> <property name="invoiceaddress" type="int"> <column name="invoiceaddress" /> </property> <property name="ouraccountnum" type="java.lang.string"> <column name="ouraccountnum" /> </property> <property name="salespoolid" type="java.lang.string"> <column name="salespoolid" /> </property> <property name="incltax" type="int"> <column name="incltax" /> </property> <property name="custitemgroupid" type="java.lang.string"> <column name="custitemgroupid" /> </property> <property name="numbersequencegroup" type="java.lang.string"> <column name="numbersequencegroup" /> </property> <property name="paymdayid" type="java.lang.string"> <column name="paymdayid" /> </property> <property name="lineofbusinessid" type="java.lang.string"> <column name="lineofbusinessid" /> </property> <property name="destinationcodeid" type="java.lang.string"> <column name="destinationcodeid" /> </property> <property name="girotype" type="int"> <column name="girotype" /> </property> <property name="suppitemgroupid" type="java.lang.string"> <column name="suppitemgroupid" /> </property> <property name="girotypeinterestnote" type="int"> <column name="girotypeinterestnote" /> </property> <property name="taxlicensenum" type="java.lang.string"> <column name="taxlicensenum" /> </property> <property name="websalesorderdisplay" type="int"> <column name="websalesorderdisplay" /> </property> <property name="paymspec" type="java.lang.string"> <column name="paymspec" /> </property> <property name="bankcentralbankpurposetext" type="java.lang.string"> <column name="bankcentralbankpurposetext" /> </property> <property name="bankcentralbankpurposecode" type="java.lang.string"> <column name="bankcentralbankpurposecode" /> </property> <property name="intercompanyallowindirectcreation" type="int"> <column name="intercompanyallowindirectcreation" /> </property> <property name="packmaterialfeelicensenum" type="java.lang.string"> <column name="packmaterialfeelicensenum" /> </property> <property name="taxbordernumberfi" type="java.lang.string"> <column name="taxbordernumberfi" /> </property> <property name="einvoiceeannum" type="java.lang.string"> <column name="einvoiceeannum" /> </property> <property name="fiscalcode" type="java.lang.string"> <column name="fiscalcode" /> </property> <property name="dlvreason" type="java.lang.string"> <column name="dlvreason" /> </property> <property name="forecastdmpinclude" type="int"> <column name="forecastdmpinclude" /> </property> <property name="girotypecollectionletter" type="int"> <column name="girotypecollectionletter" /> </property> <property name="salescalendarid" type="java.lang.string"> <column name="salescalendarid" /> </property> <property name="custclassificationid" type="java.lang.string"> <column name="custclassificationid" /> </property> <property name="intercompanydirectdelivery" type="int"> <column name="intercompanydirectdelivery" /> </property> <property name="enterprisenumber" type="java.lang.string"> <column name="enterprisenumber" /> </property> <property name="shipcarrieraccount" type="java.lang.string"> <column name="shipcarrieraccount" /> </property> <property name="girotypeprojinvoice" type="int"> <column name="girotypeprojinvoice" /> </property> <property name="inventsiteid" type="java.lang.string"> <column name="inventsiteid" /> </property> <property name="orderentrydeadlinegroupid" type="java.lang.string"> <column name="orderentrydeadlinegroupid" /> </property> <property name="shipcarrierid" type="java.lang.string"> <column name="shipcarrierid" /> </property> <property name="shipcarrierfuelsurcharge" type="int"> <column name="shipcarrierfuelsurcharge" /> </property> <property name="shipcarrierblindshipment" type="int"> <column name="shipcarrierblindshipment" /> </property> <property name="shipcarrieraccountcode" type="java.lang.string"> <column name="shipcarrieraccountcode" /> </property> <property name="girotypefreetextinvoice" type="int"> <column name="girotypefreetextinvoice" /> </property> <property name="syncentityid" type="java.lang.string"> <column name="syncentityid" /> </property> <property name="syncversion" type="long"> <column name="syncversion" /> </property> <property name="memo" type="java.lang.string"> <column name="memo" /> </property> <property name="salesdistrictid" type="java.lang.string"> <column name="salesdistrictid" /> </property> <property name="segmentid" type="java.lang.string"> <column name="segmentid" /> </property> <property name="subsegmentid" type="java.lang.string"> <column name="subsegmentid" /> </property> <property name="rfiditemtagging" type="int"> <column name="rfiditemtagging" /> </property> <property name="rfidcasetagging" type="int"> <column name="rfidcasetagging" /> </property> <property name="rfidpallettagging" type="int"> <column name="rfidpallettagging" /> </property> <property name="companychainid" type="java.lang.string"> <column name="companychainid" /> </property> <property name="companyidsiret" type="java.lang.string"> <column name="companyidsiret" /> </property> <property name="party" type="long"> <column name="party" /> </property> <property name="identificationnumber" type="java.lang.string"> <column name="identificationnumber" /> </property> <property name="partycountry" type="java.lang.string"> <column name="partycountry" /> </property> <property name="partystate" type="java.lang.string"> <column name="partystate" /> </property> <property name="paymidtype" type="java.lang.string"> <column name="paymidtype" /> </property> <property name="factoringaccount" type="java.lang.string"> <column name="factoringaccount" /> </property> <property name="defaultdimension" type="long"> <column name="defaultdimension" /> </property> <property name="custexcludecollectionfee" type="int"> <column name="custexcludecollectionfee" /> </property> <property name="custexcludeinterestcharges" type="int"> <column name="custexcludeinterestcharges" /> </property> <property name="companynafcode" type="long"> <column name="companynafcode" /> </property> <property name="bankcustpaymidtable" type="long"> <column name="bankcustpaymidtable" /> </property> <property name="girotypeaccountstatement" type="int"> <column name="girotypeaccountstatement" /> </property> <property name="maincontactworker" type="long"> <column name="maincontactworker" /> </property> <property name="creditcardaddressverification" type="int"> <column name="creditcardaddressverification" /> </property> <property name="creditcardcvc" type="int"> <column name="creditcardcvc" /> </property> <property name="creditcardaddressverificationvoid" type="int"> <column name="creditcardaddressverificationvoid" /> </property> <property name="creditcardaddressverificationlevel" type="int"> <column name="creditcardaddressverificationlevel" /> </property> <property name="companytypemx" type="int"> <column name="companytypemx" /> </property> <property name="rfcmx" type="java.lang.string"> <column name="rfcmx" /> </property> <property name="curpmx" type="java.lang.string"> <column name="curpmx" /> </property> <property name="stateinscriptionmx" type="java.lang.string"> <column name="stateinscriptionmx" /> </property> <property name="residenceforeigncountryregionidit" type="java.lang.string"> <column name="residenceforeigncountryregionidit" /> </property> <property name="birthcountycodeit" type="java.lang.string"> <column name="birthcountycodeit" /> </property> <property name="birthdateit" type="java.util.date"> <column name="birthdateit" /> </property> <property name="birthplaceit" type="java.lang.string"> <column name="birthplaceit" /> </property> <property name="einvoice" type="int"> <column name="einvoice" /> </property> <property name="ccmnumbr" type="java.lang.string"> <column name="ccmnumbr" /> </property> <property name="cnpjcpfnumbr" type="java.lang.string"> <column name="cnpjcpfnumbr" /> </property> <property name="pbacustgroupid" type="java.lang.string"> <column name="pbacustgroupid" /> </property> <property name="ienumbr" type="java.lang.string"> <column name="ienumbr" /> </property> <property name="suframanumberbr" type="java.lang.string"> <column name="suframanumberbr" /> </property> <property name="suframabr" type="int"> <column name="suframabr" /> </property> <property name="custfinaluserbr" type="int"> <column name="custfinaluserbr" /> </property> <property name="interestcodebr" type="java.lang.string"> <column name="interestcodebr" /> </property> <property name="finecodebr" type="java.lang.string"> <column name="finecodebr" /> </property> <property name="suframapiscofinsbr" type="int"> <column name="suframapiscofinsbr" /> </property> <property name="taxwithholdcalculateth" type="int"> <column name="taxwithholdcalculateth" /> </property> <property name="taxwithholdgroupth" type="java.lang.string"> <column name="taxwithholdgroupth" /> </property> <property name="consdayjp" type="int"> <column name="consdayjp" /> </property> <property name="nitbr" type="java.lang.string"> <column name="nitbr" /> </property> <property name="inssceibr" type="java.lang.string"> <column name="inssceibr" /> </property> <property name="cnaebr" type="java.lang.string"> <column name="cnaebr" /> </property> <property name="icmscontributorbr" type="int"> <column name="icmscontributorbr" /> </property> <property name="servicecodeondlvaddressbr" type="int"> <column name="servicecodeondlvaddressbr" /> </property> <property name="inventprofiletyperu" type="int"> <column name="inventprofiletyperu" /> </property> <property name="inventprofileidru" type="java.lang.string"> <column name="inventprofileidru" /> </property> <property name="taxwithholdcalculatein" type="int"> <column name="taxwithholdcalculatein" /> </property> <property name="unitedvatinvoicelt" type="int"> <column name="unitedvatinvoicelt" /> </property> <property name="enterprisecode" type="java.lang.string"> <column name="enterprisecode" /> </property> <property name="commercialregistersection" type="java.lang.string"> <column name="commercialregistersection" /> </property> <property name="commercialregisterinsetnumber" type="java.lang.string"> <column name="commercialregisterinsetnumber" /> </property> <property name="commercialregister" type="java.lang.string"> <column name="commercialregister" /> </property> <property name="regnumw" type="java.lang.string"> <column name="regnumw" /> </property> <property name="isresidentlv" type="int"> <column name="isresidentlv" /> </property> <property name="intbanklv" type="java.lang.string"> <column name="intbanklv" /> </property> <property name="paymentreferenceee" type="java.lang.string"> <column name="paymentreferenceee" /> </property> <property name="packagedepositexcemptpl" type="int"> <column name="packagedepositexcemptpl" /> </property> <property name="fednonfedindicator" type="int"> <column name="fednonfedindicator" /> </property> <property name="irs1099cindicator" type="int"> <column name="irs1099cindicator" /> </property> <property name="agencylocationcode" type="java.lang.string"> <column name="agencylocationcode" /> </property> <property name="federalcomments" type="java.lang.string"> <column name="federalcomments" /> </property> <property name="usepurchrequest" type="int"> <column name="usepurchrequest" /> </property> <property name="mcrmergedparent" type="java.lang.string"> <column name="mcrmergedparent" /> </property> <property name="mcrmergedroot" type="java.lang.string"> <column name="mcrmergedroot" /> </property> <property name="affiliatedru" type="int"> <column name="affiliatedru" /> </property> <property name="cashdiscbasedays" type="int"> <column name="cashdiscbasedays" /> </property> <property name="custtradingpartnercode" type="long"> <column name="custtradingpartnercode" /> </property> <property name="custwhtcontributiontypebr" type="int"> <column name="custwhtcontributiontypebr" /> </property> <property name="daxintegrationid" type="java.lang.string"> <column name="daxintegrationid" /> </property> <property name="defaultdirectdebitmandate" type="long"> <column name="defaultdirectdebitmandate" /> </property> <property name="defaultinventstatusid" type="java.lang.string"> <column name="defaultinventstatusid" /> </property> <property name="entrycertificaterequiredw" type="int"> <column name="entrycertificaterequiredw" /> </property> <property name="exportsalespl" type="int"> <column name="exportsalespl" /> </property> <property name="expressbilloflading" type="int"> <column name="expressbilloflading" /> </property> <property name="fiscaldoctypepl" type="int"> <column name="fiscaldoctypepl" /> </property> <property name="foreignresidentru" type="int"> <column name="foreignresidentru" /> </property> <property name="generateincomingfiscaldocumentbr" type="int"> <column name="generateincomingfiscaldocumentbr" /> </property> <property name="invoicepostingtyperu" type="int"> <column name="invoicepostingtyperu" /> </property> <property name="issueownentrycertificatew" type="int"> <column name="issueownentrycertificatew" /> </property> <property name="issuercountryhu" type="java.lang.string"> <column name="issuercountryhu" /> </property> <property name="lvpaymtranscodes" type="long"> <column name="lvpaymtranscodes" /> </property> <property name="mandatoryvatdatepl" type="int"> <column name="mandatoryvatdatepl" /> </property> <property name="passportnohu" type="java.lang.string"> <column name="passportnohu" /> </property> <property name="pdscustrebategroupid" type="java.lang.string"> <column name="pdscustrebategroupid" /> </property> <property name="pdsfreightaccrued" type="int"> <column name="pdsfreightaccrued" /> </property> <property name="pdsrebatetmagroup" type="java.lang.string"> <column name="pdsrebatetmagroup" /> </property> <property name="taxperiodpaymentcodepl" type="java.lang.string"> <column name="taxperiodpaymentcodepl" /> </property> <property name="usecashdisc" type="int"> <column name="usecashdisc" /> </property> <property name="modifieddatetime" type="java.util.date"> <column name="modifieddatetime" /> </property> <property name="delmodifiedtime" type="int"> <column name="delmodifiedtime" /> </property> <property name="modifiedby" type="java.lang.string"> <column name="modifiedby" /> </property> <property name="createddatetime" type="java.util.date"> <column name="createddatetime" /> </property> <property name="delcreatedtime" type="int"> <column name="delcreatedtime" /> </property> <property name="dataareaid" type="java.lang.string"> <column name="dataareaid" /> </property> <property name="recversion" type="int"> <column name="recversion" /> </property> <property name="partition" type="long"> <column name="partition" /> </property> <property name="recid" type="long"> <column name="recid" /> </property> <property name="einvoiceregisterit" type="int"> <column name="einvoiceregisterit" /> </property> <property name="foreigneridbr" type="java.lang.string"> <column name="foreigneridbr" /> </property> <property name="authorityofficeit" type="java.lang.string"> <column name="authorityofficeit" /> </property> <property name="presencetypebr" type="int"> <column name="presencetypebr" /> </property> <property name="taxgstreliefgroupheadingmy" type="long"> <column name="taxgstreliefgroupheadingmy" /> </property> </class> </hibernate-mapping>
as can see, mapping generator gets 2 things wrong: 1) identifies incorrect id; 2) interprets custtableid class one-to-many relationship, rather using id custtable class.
what proper way handle mapping, have custtableid class mapped id custtable class?
you're looking embeddedid or idclass: https://docs.jboss.org/hibernate/orm/5.0/mappingguide/en-us/html/ch06.html#identifiers-composite-nonaggregated
since judging provided xml you're using hibernate 3, should example: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id
in case can remove class , replace incorrect association with:
<composite-id> <key-property name="accountnum"/> <key-property name="partition"/> <key-property name="dataareaid"/> </composite-id>
Comments
Post a Comment