@OneToMany - list content not fetched

View: New views
3 Messages — Rating Filter:   Alert me  

@OneToMany - list content not fetched

by aleksjej :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I have a problem with @OneToMany relation when it points one of entity subclass.

I have the basinc Application entity which is connected with all objects and I want this entity to have a list of ManagedCompany entityes (not its superclass Company). Is this possible? Everything is fine when I choose List<Company>, but List<ManagedCompany> always is empty despite that entities of this type exists in database.


Source of Application entity containing ManagedCompany relation.


package org.pg.alex.fakturant.dal.model;

import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.pg.alex.fakturant.common.dto.ApplicationDTO;
import org.pg.alex.fakturant.common.dto.CompanyDTO;
import org.pg.alex.fakturant.common.dto.DateDTO;
import org.pg.alex.fakturant.common.enums.LicenseType;
import org.pg.alex.fakturant.dal.dao.util.DTOListUtil;
import org.pg.alex.fakturant.dal.intf.DTOTransformer;

/**
 * Represents an Application entity.<br/>
 * Each system entity must be associated with particular application. Application can contain references to many companies depending on the license.<br/>
 * @author alech
 */
@Entity
@Table(name="application")
@SequenceGenerator(name="application_sequence", sequenceName="application_seq")
public class Application implements DTOTransformer<ApplicationDTO> {

        @Id
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="application_sequence")
        private long id;
        @Column(unique=true, nullable=false)
        private String appKey;
        @Column(unique=true, nullable=false)
        private String appSerialKey;
        @Enumerated(EnumType.ORDINAL)
        private LicenseType license;
        private Date subscriptionStartDate;
        private Date subscriptionEndDate;
        @OneToMany
        private List<ManagedCompany> managedCompanies;
       
        [ ... ]
}


And here comes the Company base entity and its subclass - ManagedCompany.



/**
 *
 */
package org.pg.alex.fakturant.dal.model;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.Lob;
import javax.persistence.SequenceGenerator;
import org.pg.alex.fakturant.common.dto.CompanyDTO;
import org.pg.alex.fakturant.common.types.Address;
import org.pg.alex.fakturant.dal.intf.DTOTransformer;

/**
 * @author aleX
 *
 */
@Entity
@Inheritance
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@SequenceGenerator(name="company_sequence", sequenceName="company_seq")
public abstract class Company extends AbstractEntity implements DTOTransformer<CompanyDTO> {
       
        @Id
        @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="company_sequence")
        protected long id;
        @Embedded
        protected Address address;
        protected String nip;
        protected String phoneNumber;
        protected String website;
        protected String email;
        protected String additionalInfo;
        @Lob
        protected byte[] logoImage;
       
        public Company() {
                // TODO Auto-generated constructor stub
        }

        [ .. ]


}

----------------------------------------------

/**
 *
 */
package org.pg.alex.fakturant.dal.model;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

/**
 * @author alex
 *
 */
@Entity
@DiscriminatorValue("2")
public class ManagedCompany extends Company {

}




Probably its simple but I got a little bit confused, just need another developer to take look at this:) Any posts apreciated!

Re: @OneToMany - list content not fetched

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This should work.  What SQL is generated when you access the 1-m? (turn on logging)

What JPA provider are you using?

Re: @OneToMany - list content not fetched

by aleksjej :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have been using hibernate bundled with jboss 5.0.1, but I have given up with single table inheritance, heart this strategy can cause such problems (subclassing, polimorphism). When I switched to TABLE_PER_CLASS strategy and used @JoinColumn everything works fine now.