Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can anyone solve my problem with many to many mapping

 
Sharath Upadhya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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();
}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic