Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

can anyone solve my problem with many to many mapping

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created two entities, namely Country, Drug, and I have established a manytomany relationship between them.
So I have the third table 'country_brand_drug_name' generated. my problem is I am not able to access the third table with jpa implementation. when I try to save the records in the third table, it is not giving any exception, but it is simply not saving, and if I insert some records manually into the third table, and try to read them with jpa implementation, it is not reading, and giving null pointer exception.
I am using openJpa for jpa implementation.

please help me with a solution

@Entity
@Table(name = "brand_drug_name")
public class BrandDrugName {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "brand_name")
private String brandDrugName;

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REMOVE })
@JoinTable(name = "country_brand_drug_name", joinColumns = @JoinColumn(name = "brand_drug_name_id"),
inverseJoinColumns = @JoinColumn(name = "country_id"))
private Set<Country> countrySet;

getters() and setters()..............
}

@Entity
@Table(name = "country")
public class Country extends CompanyEntity{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "name",nullable = false)
private String name;
private String code;
private String description;

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REMOVE }, mappedBy = "countrySet")
private Set<BrandDrugName> brandDrugNameSet

getters() and setters()..........

}

public class JPASave {
public void save(long countryId, Set<BrandDrugName> drugSet) {
Country country = em.find(Country.class, countryId);
country.setBrandDrugNameSet(drugSet);
em.getTransaction().begin();
em.merge(country);
em.getTransaction.commit();
}

}
 
Let nothing stop you! Not even this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic