Cannot find the expected secondary table while generating classes automatically

I am using intellij and new to hibernate. While generating class for branch table and executing I get an exception:

  • The branch table contains the following columns: id, institute_id, street_id, name. id is a foreign key to super table (I have 2 table that share the same id sequence). institute_id is a foreign key to institute table. street_id is a foreign key to street table.
  • The Branch class that created by intellij is:

    @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;
     }
    }
    
  • This is the code I execute (intellij created this automatically):

    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();
        }
    }
    
  • I got the following exception:

    org.hibernate.AnnotationException: Cannot find the expected secondary table: no branch available for GreenWheels.Data.Entities.Branch
    

UPDATE When I generated entities with xml per entity, it created the following xml for branch and now it works. How is that?..

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>

Answers


The generated class is incorrect. I don't know exactly what the message means, but what's sure is that the entity is not mapped correctly: the institute_id is mapped twice: once as a Column and once as a JoinColumn. Same for streetId. It also adds attributes which are not needed. Here's how the class should look like:

@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;
 }
}

My guess is that the error is called by the table = "branch" attributes of the JoinColumn annotations.


Need Your Help

Sorting strings by characters first

c++ string sorting comparison stdstring

I am using std::map in C++ and it's sorting keys in alphabetical way, like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.