在自动生成类时无法找到预期的辅助表

时间:2011-12-17 13:11:47

标签: java hibernate intellij-idea

我正在使用intellij和new来进行休眠。在为分支表生成类并执行时,我得到一个异常:

  • 分支表包含以下列:id, institute_id, street_id, name
    id是超级表的外键(我有2个表共享相同的id序列) institute_id是制定表格的外键 street_id是街道表的外键。
  • intellij创建的Branch类是:

    @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
    

更新
当我使用每个实体的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>

1 个答案:

答案 0 :(得分:1)

生成的类不正确。我不确切地知道消息的含义,但确切的是实体未正确映射:institute_id被映射两次:一次为Column,一次为JoinColumnstreetId也是如此。它还添加了不需要的属性。这是课程的样子:

@Entity
public class Branch {
 private long id;

 @Column(name = "id", nullable = false, length = 19, precision = 0)
 @Id
 public long getId() {
    return id;
 }

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

 private String name;

 @Column(name = "name", nullable = false, length = 50)
 public String getName() {
    return name;
 }

 public void setName(String name) {
    this.name = name;
 }

 private Institute institute;

 @ManyToOne
 @JoinColumn(name = "institute_id", nullable = false)
 public Institute getInstitute() {
    return institute;
 }

 public void setInstitute(Institute institute) {
    this.institute = institute;
 }

 private Street street;

 @ManyToOne
 @JoinColumn(name = "street_id")
 public Street getStreet() {
    return street;
 }

 public void setStreet(Street street) {
    this.street = street;
 }
}

我的猜测是错误是由JoinColumn注释的table = "branch"属性调用的。