It's not a secret anymore!*
The moose likes Object Relational Mapping and the fly likes can anyone solve my problem with many to many mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "can anyone solve my problem with many to many mapping" Watch "can anyone solve my problem with many to many mapping" New topic
Author

can anyone solve my problem with many to many mapping

Sharath Upadhya
Greenhorn

Joined: Dec 01, 2009
Posts: 9
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();
}

}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can anyone solve my problem with many to many mapping
 
Similar Threads
inheritance and schema creation using JPA annotations
JOINED strategy not sufficient?
Orphan delete doesn't seem to be working properly
@ManyToMany and Select query
Cannot add or update a child row: a foreign key constraint fails JPA/Hibernate