我正在尝试从Hibernate生成MySQL表。我的实体之间有一对一的关系。这是代码: -
BuyerPartyDetails类: -
@Entity
@Table(name = "buyerPartyDetails")
public class BuyerPartyDetails {
/**
* For annotation all subsequent properties name must be presented as same as in XML file.
*/
// for buyerPartyDetails table primary key
int buyerPartyDetailsId;
// Xml node and buyerPartyDetails.BuyerOrganisationName column
@XmlElement
private String BuyerOrganisationName;
// Xml node and buyerPartyDetails.BuyerPartyIdentifier column
//@XmlElement
@Transient
private String BuyerPartyIdentifier;
// it is for database column name. Otherwise it conflicts with xml annotation
private String buyid;
@Column(name = "buyerPartyIdentifier")
public String getBuyid() {
return buyid;
}
public void setBuyid(String buyid) {
this.buyid = buyid;
}
// Xml node and buyerPostalAddressDetails table
// Set provides annotation for xml parser.
// Get provides annotation for Database framework (Entity).
private BuyerPostalAddressDetails buyerPostalAddressDetails;
// Xml node and invoiceDetails table
private Set<InvoiceDetails> invoiceDetails = new HashSet<InvoiceDetails>(0);
// must annotate in getter method for database
@OneToMany(fetch = FetchType.LAZY, mappedBy = "buyerPartyDetails")
public Set<InvoiceDetails> getInvoiceDetails() {
return invoiceDetails;
}
@XmlElements({ @XmlElement(name = "buyerPostalAddressDetails", type = BuyerPartyDetails.class),
@XmlElement(name = "buyerPostalAddressDetails", type = BuyerPartyDetails.class) })
public void setInvoiceDetails(Set<InvoiceDetails> invoiceDetails) {
this.invoiceDetails = invoiceDetails;
}
//@Column(name = "XmlFileName")
String xmlFileName;
@Column(name = "XmlFileName")
public String getXmlFileName() {
return xmlFileName;
}
public void setXmlFileName(String xmlFileName) {
this.xmlFileName = xmlFileName;
}
/**
* All properties setter and getter
*/
// must annotate in getter method for database
// buyerPartyDetails.BuyerPartyDetails_id table column.
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "buyerPartyDetails_id", unique = true, nullable = false)
public int getBuyerPartyDetails_id() {
return buyerPartyDetailsId;
}
public void setBuyerPartyDetails_id(int buyerPartyDetails_id) {
buyerPartyDetailsId = buyerPartyDetails_id;
}
// must annotate in getter method for database
// buyerPartyDetails table has one to one relation with buyerPostalAddressDetails table
@OneToOne(fetch = FetchType.LAZY, mappedBy = "buyerPartyDetails", cascade = CascadeType.ALL)
public BuyerPostalAddressDetails getBuyerPostalAddressDetails() {
return buyerPostalAddressDetails;
}
@XmlElements({ @XmlElement(name = "BuyerPostalAddressDetails", type = BuyerPostalAddressDetails.class),
@XmlElement(name = "BuyerPartyDetails", type = BuyerPartyDetails.class) })
public void setBuyerPostalAddressDetails(
BuyerPostalAddressDetails buyerPostalAddressDetails) {
this.buyerPostalAddressDetails = buyerPostalAddressDetails;
}
// must annotate in getter method for database
// buyerPartyDetails.BuyerOrganisationName column
@Column(name = "buyerOrganisationName")
public String getBuyerOrganisationName ()
{
return BuyerOrganisationName;
}
public void setBuyerOrganisationName (String BuyerOrganisationName)
{
this.BuyerOrganisationName = BuyerOrganisationName;
}
// must annotate in getter method for database
// buyerPartyDetails.BuyerPartyIdentifier column
//@Column(name = "BuyerPartyIdentifier")
@Transient
public String getBuyerPartyIdentifier ()
{
return BuyerPartyIdentifier;
}
@Transient
@XmlElement(name = "BuyerPartyIdentifier")
public void setBuyerPartyIdentifier (String BuyerPartyIdentifier)
{
this.BuyerPartyIdentifier = BuyerPartyIdentifier;
buyid = BuyerPartyIdentifier;
}
@Override
public String toString() {
return "BuyerPartyDetails [buyerPostalAddressDetails="
+ buyerPostalAddressDetails + ", BuyerOrganisationName="
+ BuyerOrganisationName + ", BuyerPartyIdentifier="
+ BuyerPartyIdentifier + "]";
}
}
BuyerPostalAddressDetails: -
@Entity
@Table(name = "buyerPostalAddressDetails")
public class BuyerPostalAddressDetails {
int buyerPartyDetailsId;
@XmlElement
private String BuyerTownName;
@XmlElement
private String BuyerPostCodeIdentifier;
@XmlElement
private String BuyerStreetName;
private BuyerPartyDetails buyerPartyDetails;
// must annotate in getter method for database
// buyerPostalAddressDetails table has one to one relation with buyerPartyDetails table.
// buyerPostalAddressDetails.BuyerPartyDetails_id table column.
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "buyerPartyDetails"))
@Id
@GeneratedValue(generator = "generator")
public int getBuyerPartyDetails_id() {
return buyerPartyDetailsId;
}
public void setBuyerPartyDetails_id(int BuyerPartyDetails_id) {
BuyerPartyDetails_id = BuyerPartyDetails_id;
}
public String getBuyerTownName() {
return BuyerTownName;
}
public void setBuyerTownName(String buyerTownName) {
BuyerTownName = buyerTownName;
}
public String getBuyerPostCodeIdentifier() {
return BuyerPostCodeIdentifier;
}
public void setBuyerPostCodeIdentifier(String buyerPostCodeIdentifier) {
BuyerPostCodeIdentifier = buyerPostCodeIdentifier;
}
public String getBuyerStreetName() {
return BuyerStreetName;
}
public void setBuyerStreetName(String buyerStreetName) {
BuyerStreetName = buyerStreetName;
}
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public BuyerPartyDetails getBuyerPartyDetails() {
return buyerPartyDetails;
}
public void setBuyerPartyDetails(BuyerPartyDetails buyerPartyDetails) {
this.buyerPartyDetails = buyerPartyDetails;
}
}
当我运行代码时。我可以看到MySQL表是在数据库中生成的: -
解释表买家派对: -
解释表买家的帖子: -
当我尝试将数据插入表格时,我得到一个例外: -
实例的标识符 com.ma.home.model.BuyerPostalAddressDetails从1更改为0
我的桌子有什么问题以及如何修理它?
用于解析的XML文件: -
这是一个巨大的文件。所以我只从XML文件中共享一个节点: -
<Finvoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.01" xsi:noNamespaceSchemaLocation="Finvoice.xsd">
<SellerPartyDetails>
<SellerPartyIdentifier>0101263-6</SellerPartyIdentifier>
<SellerOrganisationName>Espoon kaupunki</SellerOrganisationName>
</SellerPartyDetails>
<SellerOrganisationUnitNumber>00370101263600228</SellerOrganisationUnitNumber>
<InvoiceRecipientLanguageCode>fi</InvoiceRecipientLanguageCode>
<BuyerPartyDetails>
<BuyerPartyIdentifier>12227-000X</BuyerPartyIdentifier>
<BuyerOrganisationName>Test user</BuyerOrganisationName>
<BuyerPostalAddressDetails>
<BuyerStreetName>KOLKEKANNAKSENTIE 10 D 7</BuyerStreetName>
<BuyerTownName>ESPOO</BuyerTownName>
<BuyerPostCodeIdentifier>02720</BuyerPostCodeIdentifier>
</BuyerPostalAddressDetails>
</BuyerPartyDetails>
<InvoiceDetails>
<InvoiceTypeCode>INV01</InvoiceTypeCode>
<InvoiceTypeText>LASKU</InvoiceTypeText>
<OriginCode>Original</OriginCode>
<InvoiceNumber>1322818237</InvoiceNumber>
<InvoiceDate Format="CCYYMMDD">20131024</InvoiceDate>
<SellersBuyerIdentifier>134151</SellersBuyerIdentifier>
<AgreementIdentifier>228004272</AgreementIdentifier>
<DefinitionDetails>
<DefinitionHeaderText DefinitionCode="AddressDate">Osoitteen voimaantulo</DefinitionHeaderText>
<DefinitionValue>20040902</DefinitionValue>
</DefinitionDetails>
<DefinitionDetails>
<DefinitionHeaderText DefinitionCode="Cost">Huomautuskulut</DefinitionHeaderText>
<DefinitionValue>0,00</DefinitionValue>
</DefinitionDetails>
<DefinitionDetails>
<DefinitionHeaderText DefinitionCode="Interest">Viivästyskorko</DefinitionHeaderText>
<DefinitionValue>0,00</DefinitionValue>
</DefinitionDetails>
<InvoiceTotalVatIncludedAmount AmountCurrencyIdentifier="EUR">252,07</InvoiceTotalVatIncludedAmount>
<InvoiceFreeText>KOTIHOIDON KÄYNTI LIPPAJÄRVI-JUPPERIN ALUE;TURVAPUHELIN OSTOPALVELU LIPPAJÄRVI-JUPPERIN ALUE;KOTIINKULJ.ATERIA (KH. AS.) LIPPAJÄRVI-JUPPERIN ALUE</InvoiceFreeText>
<PaymentTermsDetails>
<InvoiceDueDate Format="CCYYMMDD">20140224</InvoiceDueDate>
</PaymentTermsDetails>
</InvoiceDetails>
<InvoiceRow>
<RowIdentifier/>
</InvoiceRow>
<EpiDetails>
<EpiIdentificationDetails>
<EpiDate Format="CCYYMMDD">20140923</EpiDate>
<EpiReference>0</EpiReference>
</EpiIdentificationDetails>
<EpiPartyDetails>
<EpiBfiPartyDetails>
<EpiBfiIdentifier IdentificationSchemeName="BIC">BANKFIHH</EpiBfiIdentifier>
</EpiBfiPartyDetails>
<EpiBeneficiaryPartyDetails>
<EpiAccountID IdentificationSchemeName="IBAN">FI89136787</EpiAccountID>
</EpiBeneficiaryPartyDetails>
</EpiPartyDetails>
<EpiPaymentInstructionDetails>
<EpiRemittanceInfoIdentifier IdentificationSchemeName="SPY">00000082375</EpiRemittanceInfoIdentifier>
<EpiInstructedAmount AmountCurrencyIdentifier="EUR">252,07</EpiInstructedAmount>
<EpiCharge ChargeOption="SHA">SHA</EpiCharge>
<EpiDateOptionDate Format="CCYYMMDD">20140224</EpiDateOptionDate>
</EpiPaymentInstructionDetails>
</EpiDetails>
</Finvoice>