This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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();
}

}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can anyone solve my problem with many to many mapping
 
Similar Threads
inheritance and schema creation using JPA annotations
@ManyToMany and Select query
Cannot add or update a child row: a foreign key constraint fails JPA/Hibernate
JOINED strategy not sufficient?
Orphan delete doesn't seem to be working properly