0

我用hibernate从db生成java类。在配置中,我设置为生成: 在此处输入图像描述

并从每个表中获取 3 个文件。例如:

地址.java

package gen;

// Generated 16.08.2012 12:47:01 by Hibernate Tools 3.4.0.CR1

/**
* Address generated by hbm2java
*/
public class Address implements java.io.Serializable
{

private AddressId id;

public Address()
{
}

public Address(AddressId id)
{
    this.id = id;
}

public AddressId getId()
{
    return this.id;
}

public void setId(AddressId id)
{
    this.id = id;
}

}

地址Id.java

package gen;

// Generated 16.08.2012 12:47:01 by Hibernate Tools 3.4.0.CR1

/**
* AddressId generated by hbm2java
*/
public class AddressId implements java.io.Serializable
{

private String codeOkato;
private String codeKladr;
private String postalCode;
private String region;
private String house;
private String building;
private String structure;
private String apartment;
private String note;

public AddressId()
{
}

public AddressId(String codeOkato, String codeKladr, String postalCode, String region, String house, String building,
        String structure, String apartment, String note)
{
    this.codeOkato = codeOkato;
    this.codeKladr = codeKladr;
    this.postalCode = postalCode;
    this.region = region;
    this.house = house;
    this.building = building;
    this.structure = structure;
    this.apartment = apartment;
    this.note = note;
}

public String getCodeOkato()
{
    return this.codeOkato;
}

public void setCodeOkato(String codeOkato)
{
    this.codeOkato = codeOkato;
}

public String getCodeKladr()
{
    return this.codeKladr;
}

public void setCodeKladr(String codeKladr)
{
    this.codeKladr = codeKladr;
}

public String getPostalCode()
{
    return this.postalCode;
}

public void setPostalCode(String postalCode)
{
    this.postalCode = postalCode;
}

public String getRegion()
{
    return this.region;
}

public void setRegion(String region)
{
    this.region = region;
}

public String getHouse()
{
    return this.house;
}

public void setHouse(String house)
{
    this.house = house;
}

public String getBuilding()
{
    return this.building;
}

public void setBuilding(String building)
{
    this.building = building;
}

public String getStructure()
{
    return this.structure;
}

public void setStructure(String structure)
{
    this.structure = structure;
}

public String getApartment()
{
    return this.apartment;
}

public void setApartment(String apartment)
{
    this.apartment = apartment;
}

public String getNote()
{
    return this.note;
}

public void setNote(String note)
{
    this.note = note;
}

public boolean equals(Object other)
{
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof AddressId))
        return false;
    AddressId castOther = (AddressId) other;

    return ((this.getCodeOkato() == castOther.getCodeOkato()) || (this.getCodeOkato() != null
            && castOther.getCodeOkato() != null && this.getCodeOkato().equals(castOther.getCodeOkato())))
            && ((this.getCodeKladr() == castOther.getCodeKladr()) || (this.getCodeKladr() != null
                    && castOther.getCodeKladr() != null && this.getCodeKladr().equals(castOther.getCodeKladr())))
            && ((this.getPostalCode() == castOther.getPostalCode()) || (this.getPostalCode() != null
                    && castOther.getPostalCode() != null && this.getPostalCode().equals(castOther.getPostalCode())))
            && ((this.getRegion() == castOther.getRegion()) || (this.getRegion() != null && castOther.getRegion() != null && this
                    .getRegion().equals(castOther.getRegion())))
            && ((this.getHouse() == castOther.getHouse()) || (this.getHouse() != null && castOther.getHouse() != null && this
                    .getHouse().equals(castOther.getHouse())))
            && ((this.getBuilding() == castOther.getBuilding()) || (this.getBuilding() != null
                    && castOther.getBuilding() != null && this.getBuilding().equals(castOther.getBuilding())))
            && ((this.getStructure() == castOther.getStructure()) || (this.getStructure() != null
                    && castOther.getStructure() != null && this.getStructure().equals(castOther.getStructure())))
            && ((this.getApartment() == castOther.getApartment()) || (this.getApartment() != null
                    && castOther.getApartment() != null && this.getApartment().equals(castOther.getApartment())))
            && ((this.getNote() == castOther.getNote()) || (this.getNote() != null && castOther.getNote() != null && this
                    .getNote().equals(castOther.getNote())));
}

public int hashCode()
{
    int result = 17;

    result = 37 * result + (getCodeOkato() == null ? 0 : this.getCodeOkato().hashCode());
    result = 37 * result + (getCodeKladr() == null ? 0 : this.getCodeKladr().hashCode());
    result = 37 * result + (getPostalCode() == null ? 0 : this.getPostalCode().hashCode());
    result = 37 * result + (getRegion() == null ? 0 : this.getRegion().hashCode());
    result = 37 * result + (getHouse() == null ? 0 : this.getHouse().hashCode());
    result = 37 * result + (getBuilding() == null ? 0 : this.getBuilding().hashCode());
    result = 37 * result + (getStructure() == null ? 0 : this.getStructure().hashCode());
    result = 37 * result + (getApartment() == null ? 0 : this.getApartment().hashCode());
    result = 37 * result + (getNote() == null ? 0 : this.getNote().hashCode());
    return result;
}

}

地址.hbm.xml

 <?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 16.08.2012 12:47:01 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="gen.Address" table="address">
    <composite-id name="id" class="gen.AddressId">
        <key-property name="codeOkato" type="string">
            <column name="code_okato" length="11" />
        </key-property>
        <key-property name="codeKladr" type="string">
            <column name="code_kladr" length="20" />
        </key-property>
        <key-property name="postalCode" type="string">
            <column name="postal_code" length="6" />
        </key-property>
        <key-property name="region" type="string">
            <column name="region" />
        </key-property>
        <key-property name="house" type="string">
            <column name="house" />
        </key-property>
        <key-property name="building" type="string">
            <column name="building" />
        </key-property>
        <key-property name="structure" type="string">
            <column name="structure" />
        </key-property>
        <key-property name="apartment" type="string">
            <column name="apartment" />
        </key-property>
        <key-property name="note" type="string">
            <column name="note" length="1500" />
        </key-property>
    </composite-id>
</class>
</hibernate-mapping>

您如何在映射文件中看到使用类 Address.java。但是对于这个类,我无法设置或获取任何参数,例如apartmentor building。只有在 AddressId 类中。
我看到的所有带有hibernate的例子都只使用一个java类。冬眠的第二类有什么功能。所以我想做所有正确的事情。以及我将如何生成 java 类或使用我生成的东西来处理 xml 文件和数据库?

另一个问题是如何在 cenerated java 类中自动创建注释?

4

1 回答 1

1

要生成注释而不是 hbm.xml,您应该取消选中Hibernate XML mappings(.hbm.xml)并选择Generate EJB3 annotations

当表的主键由多列组成时,Hibernate 会为 ID 生成单独的类。当表根本没有主键时,它也会这样做。

于 2012-08-16T07:42:07.823 回答