0
我正在使用intellij,並且是用於休眠的新類。同時爲分支表類和執行我得到一個異常:在自動生成類時找不到預期的輔助表
- 分支表包含以下幾列:
id, institute_id, street_id, name
。
id
是超級表的外鍵(我有2個表共享相同的id序列)。
institute_id
是實現表的外鍵。
street_id
是街道表的外鍵。 通過的IntelliJ建立的分支類是:
@Entity public class Branch { private long id; @javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true, length = 19, precision = 0) @Id public long getId() { return id; } public void setId(long id) { this.id = id; } private long instituteId; @javax.persistence.Column(name = "institute_id", nullable = false, insertable = true, updatable = true, length = 19, precision = 0) @Basic public long getInstituteId() { return instituteId; } public void setInstituteId(long instituteId) { this.instituteId = instituteId; } private long streetId; @javax.persistence.Column(name = "street_id", nullable = true, insertable = true, updatable = true, length = 19, precision = 0) @Basic public long getStreetId() { return streetId; } public void setStreetId(long streetId) { this.streetId = streetId; } private String name; @javax.persistence.Column(name = "name", nullable = false, insertable = true, updatable = true, length = 50, precision = 0) @Basic public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Branch branch = (Branch) o; if (id != branch.id) return false; if (instituteId != branch.instituteId) return false; if (streetId != branch.streetId) return false; if (name != null ? !name.equals(branch.name) : branch.name != null) return false; return true; } @Override public int hashCode() { int result = (int) (id^(id >>> 32)); result = 31 * result + (int) (instituteId^(instituteId >>> 32)); result = 31 * result + (int) (streetId^(streetId >>> 32)); result = 31 * result + (name != null ? name.hashCode() : 0); return result; } private Institute instituteByInstituteId; @ManyToOne public @javax.persistence.JoinColumn(name = "institute_id", referencedColumnName = "id", nullable = false, table = "branch") Institute getInstituteByInstituteId() { return instituteByInstituteId; } public void setInstituteByInstituteId(Institute instituteByInstituteId) { this.instituteByInstituteId = instituteByInstituteId; } private Street streetByStreetId; @ManyToOne public @javax.persistence.JoinColumn(name = "street_id", referencedColumnName = "id", table = "branch") Street getStreetByStreetId() { return streetByStreetId; } public void setStreetByStreetId(Street streetByStreetId) { this.streetByStreetId = streetByStreetId; } }
這是我執行代碼(的IntelliJ創造了這個自動):
public static void main(final String[] args) throws Exception { final Session session = getSession(); try { System.out.println("querying all the managed entities..."); final Map metadataMap = session.getSessionFactory().getAllClassMetadata(); for (Object key : metadataMap.keySet()) { final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key); final String entityName = classMetadata.getEntityName(); final Query query = session.createQuery("from " + entityName); System.out.println("executing: " + query.getQueryString()); for (Object o : query.list()) { System.out.println(" " + o); } } } finally { session.close(); } }
我有以下異常:
org.hibernate.AnnotationException: Cannot find the expected secondary table: no branch available for GreenWheels.Data.Entities.Branch
UPDATE
當我爲每個實體生成xml實體時,它爲分支創建了以下xml,現在它工作。那是怎麼回事?
Branch.nbm.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">
<hibernate-mapping>
<class name="GreenWheels.Data.Entities.Branch" table="branch" catalog="greenwheels">
<id name="id">
<column name="id" sql-type="bigint" length="19" not-null="true"/>
</id>
<property name="instituteId">
<column name="institute_id" sql-type="bigint" length="19" not-null="true"/>
</property>
<property name="streetId">
<column name="street_id" sql-type="bigint" length="19"/>
</property>
<property name="name">
<column name="name" sql-type="varchar" length="50" not-null="true"/>
</property>
<many-to-one name="instituteByInstituteId" class="GreenWheels.Data.Entities.Institute">
<column name="institute_id" not-null="true"/>
</many-to-one>
<many-to-one name="streetByStreetId" class="GreenWheels.Data.Entities.Street">
<column name="street_id"/>
</many-to-one>
</class>
</hibernate-mapping>
這沒有工作..我仍然得到這個例外。請參閱更新。 – Naor
該解決方案適用於我。當像這樣映射一個ManyToOne關係時,除了'@ JoinColumn'條目外,我還有一個用於外鍵的'@ Column'條目。我只是刪除了@ @列,因爲它是多餘的,它的工作! – scottyseus