views:

178

answers:

1

i have a class campaign that maintains a list of AdGroupInterfaces. im going to persist its implementation

    @Entity
     @Table(name = "campaigns")
     public class Campaign implements Serializable,Comparable<Object>,CampaignInterface               {
private static final long serialVersionUID = 1L;


    @Id 
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

    @OneToMany (
        cascade = {CascadeType.ALL},
        fetch = FetchType.EAGER,
    targetEntity=AdGroupInterface.class
        )
@org.hibernate.annotations.Cascade(
value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN
)
@org.hibernate.annotations.IndexColumn(name = "CHOICE_POSITION")
 private List<AdGroupInterface> AdGroup;


public Campaign() {
    super();
}

public List<AdGroupInterface> getAdGroup() {
    return AdGroup;
}

public void setAdGroup(List<AdGroupInterface> adGroup) {
    AdGroup = adGroup;
}

public void set1AdGroup(AdGroupInterface adGroup) {
    if(AdGroup==null)
        AdGroup=new LinkedList<AdGroupInterface>();
    AdGroup.add(adGroup);
}

}

AdGroupInterface's implementation is AdGroups. when i add an adgroup to the list in campaign,

campaign c; c.getAdGroupList().add(new AdGroups()), etc and save campaign

it says"Cannot instantiate abstract class or interface :" AdGroupInterface

its not recognizing the implementation just before persisting...

Whereas Persisting adGroups separately works. when it is a member of another entity, it doesnt get persisted.

         import java.io.Serializable;
         import java.util.List;
         import javax.persistence.*;


          @Entity

          @DiscriminatorValue("1")

          @Table(name = "AdGroups")

         public class AdGroups implements Serializable,Comparable,AdGroupInterface{


/**
 * 
 */
private static final long serialVersionUID = 1L;


private Long Id;


private String Name;


private CampaignInterface Campaign;


private MonetaryValue DefaultBid;

    public AdGroups(){

    super();
}
public AdGroups( String name, CampaignInterface campaign) {
    super();
    this.Campaign=new Campaign();
    Name = name;
    this.Campaign = campaign;
    DefaultBid = defaultBid;
    AdList=adList;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="AdGroup_Id")
public Long getId() {
    return Id;
}

public void setId(Long id) {
    Id = id;
}

@Column(name="AdGroup_Name")
public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

@ManyToOne
@JoinColumn (name="Cam_ID", nullable = true,insertable = false)
public CampaignInterface getCampaign() {

    return Campaign;
}

public void setCampaign(CampaignInterface campaign) {
    this.Campaign = campaign;
}

}

what am i missing?? please look into it ...

A: 

I think that the targetEntity of your OneToMany association should be a class (maybe abstract, but not an interface). Have a look at this previous answer.

Pascal Thivent
thanks a lot for ur reply :) it gets persisted when i say getHibernateTemplate.save(adgroup); (which was an interface )but saving save(campaign) doesnt work where adgroup is its class member... it creates the third join table campaign_adgroupinterface after campaigninterface_adgroupinterface. that means campaign is recognized from campaignInterface but adgroup is not.. that means some problem when it is a data member of campaign.. thats js my guess..............
sammy
@sammy Does your implementation work or not? If it doesn't, you have to change something.
Pascal Thivent
oh yes it seems JPA doesnt support persisting interfaces in the older versions.. i dunno abt the new versions..abstract classes seem to be an option ...ill keep trying thnx
sammy
@pascal-- thnx a lot. u gave me a good direction :)we have to put @Transient on all getters and setters of interface............... and it gets stored
sammy