休止状態で 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;
}
}
AddressId.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;
}
}
Address.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 を使用しています。apartment
しかし、このクラスでは、またはのようなパラメーターを設定または取得できませんbuilding
。AddressId クラスにのみあります。
私が見ている休止状態のすべての例は、1つのJavaクラスのみを使用しています。ハイバネートがフェネレートするセカンドクラスの機能。だから私はすべてを正しくしたい。Java クラスを生成したり、生成したものを使用して xml ファイルやデータベースを操作したりするにはどうすればよいでしょうか。
そして別の質問は、生成されたJavaクラスで自動的に注釈を作成する方法ですか?