Request communication between many sleep users

In Hibernate HQL, how would you query for a many-to-many relationship. If I have a company with multiple products and other companies can offer the same product lines, I have a company, a ProductLine object, and a CompanyProductLine association table. In SQL, I can get what I need:

select * from company c where c.companyId in (select companyId from companyProductLine cpl, productline pl where cpl.productLineId = pl.productLineId and pl.name= 'some value');

      

In my problem, I can see the association defined in the Company.hbm.xml file:

<set name="productLines" 
     cascade="save-update" 
     table="CompanyProductLine">
   <key column="companyId"/>
   <many-to-many class="com.foo.ProductLine" column="productLineId" />
</set> 

      

Any HQL I seem to have come up with will generate: "pending" items or "indexes" Hibernate exception.

Thoughts on what would be the correct HQL?

+2


a source to share


1 answer


Your hql query should look like this:

from Company c join c.productLines pl where pl.name = :name

      



And the display looks like this:

<hibernate-mapping>
    <class name=com.example.ProductLine" table="productLine">
        <cache usage="read-write"/>
        <id name="id" column="id" type="long">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.example.Company" table="company">
        <cache usage="read-write" />
        <id name="id" column="id" type="long">
            <generator class="native" />
        </id>
        <set name="productLines" table="companyProductLine" lazy="false">
            <key column="companyId" />
            <many-to-many class="com.example.ProductLine" column="productLineId" />
        </set>
    </class>
</hibernate-mapping>

      

+4


a source







All Articles