0

I have a parent object lets say Campaign and child object Ad Segments. Campaign is holding one to many relation with Ad Segments. following is the annotations I defined.

class CampaignModel {
OneToMany(mappedBy="campaign", cascade=CascadeType.PERSIST, fetch = FetchType.EAGER)
@Setter
@Column(updatable=false)
private List<AdSegmentModel> segments;
.....
}

class AdSegmentModel {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="campaign_id")
@Getter
@Setter
private CampaignModel campaign;

Requirements :

  1. When we insert into Campaign Model, ad segments should also get inserted.

  2. When we retrieve Campaign Model, corrosponding ad segments should get retrieved.

  3. Ad Segments are non- updatable. When we update campaigns, nothing should happen in Ad segments table.

Issues facing:

  1. When I do CascadeType.PERSIST , campaign is getting inserted but nothing happen in Ad segments table. (Reason : https://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/). I dont know the solution as I dont want to give CascadeType.SAVE_UPDATE. Because it will cause un ecessary operations in case of update campaigns.

  2. When i Retrieve campaigns, it is giving me duplicate record. Reason is it is doing left outer join with Ad segments table and since ad segment table has multiple rows corrosponding to one row in campaign table, it is givig duplicate records.

For saving Campiagn Object :

try {
        Session session = sessionFactory 
                .getCurrentSession();
        long id = (long) session.save(newCampaign);
        return id;
    } catch (HibernateException e) {
        throw new DependencyException("Cannot save campaign", e);
    }

For getting campaign :

Session session = sessionFactory
                .getCurrentSession();
        List<Criterion> criteria = new ArrayList<>();
        criteria.add(Restrictions.eq("obfuscatedEntityId",
                obfuscatedEntityId));
        List<CampaignModel> campaignModels = HibernateUtils
                .loadObjectsFromHibernate(session, criteria, CampaignModel.class);
        System.out.println("size inside builder functionsss"+campaignModels.size());
        return campaignModels;

Read this : Hibernate : CascadeType.PERSIST does not work but CascadeType.ALL to save object My question is if on updating parent object, nothing should happen in child table which annotation should I use? Is it safer to use SAVE_UPDATE?

Please help!

Community
  • 1
  • 1
user3681970
  • 1,201
  • 4
  • 19
  • 37
  • Is "CampaignModel" just a typo? It should be "Campaign" – Magd Kudama Dec 11 '16 at 08:46
  • Its a typo. Sorry! – user3681970 Dec 11 '16 at 08:50
  • How are you using the code? Can you add an example usage that's not working? – Magd Kudama Dec 11 '16 at 08:55
  • I am so confused. Can you please explain the usage of Fetchtype.eager. Whenever the campaign model is getting loaded i want Ad segments also to get loaded. So should I mention FetchType.Eager in Campaign Model or Ad Segment Model? – user3681970 Dec 11 '16 at 08:59
  • I understand the problem but I need to see a sample non-working code. Then I will be able to help. – Magd Kudama Dec 11 '16 at 09:36
  • I have updated. The main problem resides in designing associations. Please help in that. – user3681970 Dec 11 '16 at 09:49
  • Use CascadeType.ALL . – Suresh A Dec 11 '16 at 10:19
  • @Surace Then in case of Parent object update it will try updating the child object also. I dont want this use case. My child object should not get upodated. – user3681970 Dec 11 '16 at 10:29
  • @user3681970 By the first view to your code I am bit confuse why you are not calling `commit()` after `save()` like `session.getTransaction().commit();` and second point can you please make sure by debugging your code that the object `newCampaign` must have `segments` filled. Means insert breakpoint on `long id = (long) session.save(newCampaign);` line and make sure `newCampaign` is having `segments` if its `null` or 0 in size then it will not get inserted. – Amogh Dec 11 '16 at 11:30
  • @user3681970 calling `commit()` is depends on how you configured `transaction` if you can see parent object in DB after `save()` then please make sure about second point I mentioned – Amogh Dec 11 '16 at 11:31
  • Sure Amogh. Let me check your above points. please stay tuned. – user3681970 Dec 11 '16 at 11:39
  • I checked. Child Objects are there. This issue might be because of mykong link i mentioned in the question above – user3681970 Dec 11 '16 at 11:49
  • @user3681970 yes, but I am considering you have deleted the JPA cascade – javax.persistence.CascadeType, replace it with Hibernate cascade – org.hibernate.annotations.Cascade. you have just have to use `PERSIST` instead of `SAVE_UPDATE` – Amogh Dec 11 '16 at 11:56
  • Super. You have picked up the mistake. I think its the import problem. Should I replace Java persistence entirely with hibernate? Why to mix up both? In that case you can please let me know the import required for hibernate onetomany mapping? Their documentation is poor. I could not find the import at all. – user3681970 Dec 11 '16 at 12:08
  • Everywhere i can find only javax.persistence import. What are the imports required for hibernate table, entity, onetomany and other things? – user3681970 Dec 11 '16 at 12:19

0 Answers0