java - Hibernate 创建了太多的 java 类

标签 java hibernate postgresql

我使用 hibernate 从数据库生成 java 类。在配置中我设置为生成: enter image description here

并从每个表中获取 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。但是对于这个类,我无法设置或获取任何参数,例如apartmentbuilding。仅在AddressId类中存在。
我所看到的所有 hibernate 示例都只使用一个 java 类。第二类hibernate fenerate有什么功能。 所以我想做所有正确的事情。我如何生成 java 类或使用我生成的内容来处理 xml 文件和数据库?

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

最佳答案

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

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

关于java - Hibernate 创建了太多的 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11982201/

相关文章:

oracle - JPA 2.0 Oracle DATE 时间为空

sql - postgres 错误 "collation "default.pg_catalog“编码 "UTF8"不存在”

PostgreSQL:计算一行中包含值的列数

sql - 获取表sql中每个字段的名称

java - java.util.Calendar 中的 complete() 方法

java - 如何将 Byte 数组转换为 PrivateKey 或 PublicKey 类型?

java - 如何查看 session 中是否添加或删除了所有数据?

java - 如何获取出现次数最多的对象集?

java - 如何在java中将输入的字符串传递给谷歌搜索

mysql - 如何使用 Spring 5 公开服务?