Spring + Hibernate + JPA: remove entity reference in join table - java

I'm facing a difficulty in developing a server in Spring (+ Hibernate + JPA) for a project.
The structure of the server (the part of interest in this case) is composed of catalogs composed of products that can have some related feedbacks.
Here I share the 3 entities:
Catalog.java
#Entity
#Data
#Table(name = "catalog")
public class Catalog {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(nullable = false, unique = true)
private String name;
private String description;
#ManyToOne(cascade = CascadeType.MERGE)
#JoinColumn(name = "catalog_user_id", nullable = false)
private User user;
#ManyToMany(cascade = {CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
#JoinTable(name = "catalog_product",
joinColumns = {#JoinColumn(name = "catalog_id")},
inverseJoinColumns = {#JoinColumn(name = "product_id")}
)
private List<Product> products;
public Catalog() {}
}
Product.java
#Entity
#Data
#Table(name = "product")
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(nullable = false, unique = true)
private String name;
private String description;
#Column(nullable = false, length = 1)
#MapKeyEnumerated(EnumType.ORDINAL)
private Category category;
#ManyToOne(cascade = CascadeType.MERGE)
#JoinColumn(name = "product_user_id", nullable = false)
private User user;
public Product() {}
}
Feedback.java
#Entity
#Data
#Table(name = "feedback")
public class Feedback {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne(cascade = CascadeType.MERGE)
#JoinColumn(name = "feedback_user_id", nullable = false)
private User user;
#Column(nullable = false, length = 1)
#MapKeyEnumerated(EnumType.ORDINAL)
private Rating rating;
private String text;
#ManyToOne(cascade = CascadeType.MERGE)
#JoinColumn(name = "product_id", nullable = false)
private Product product;
public Feedback() {}
}
The problem occurs when I try to delete some entities. What I want is:
when I delete a catalog also the catalog references in the "catalog_product" join table should be deleted (but the product linked with the catalog should not be deleted);
when I delete a product also the product references in the "catalog_product" join table and the feedbacks related to that product should be deleted;
when I delete a feedback nothing happens.
In the business layer I have this operations:
CatalogServiceImpl.java
#Service
public class CatalogServiceImpl implements CatalogService {
#Autowired
private CatalogDAO catalogDAO;
#Autowired
private ModelMapper mapper;
public CatalogDTO findById(Long id) {
Catalog catalog = catalogDAO.findById(id);
return mapper.map(catalog, CatalogDTO.class);
}
public CatalogDTO findByName(String name) {
Catalog catalog = catalogDAO.findByName(name);
return mapper.map(catalog, CatalogDTO.class);
}
public List<CatalogDTO> findByUserId(Long id) {
List<Catalog> catalogs = catalogDAO.findByUserId(id);
Type listCatalogsType = new TypeToken<List<CatalogDTO>>() {}.getType();
return mapper.map(catalogs, listCatalogsType);
}
public List<CatalogDTO> findAll() {
List<Catalog> catalogs = catalogDAO.findAll();
Type listCatalogsType = new TypeToken<List<CatalogDTO>>() {}.getType();
return mapper.map(catalogs, listCatalogsType);
}
public CatalogDTO createCatalog(CatalogDTO catalogDTO) {
Catalog catalog = mapper.map(catalogDTO, Catalog.class);
Catalog catalogFromDB = catalogDAO.save(catalog);
return mapper.map(catalogFromDB, CatalogDTO.class);
}
public CatalogDTO updateCatalog(CatalogDTO catalogDTO) {
Catalog catalog = mapper.map(catalogDTO, Catalog.class);
Catalog catalogFromDB;
if(catalogDAO.exists(catalog.getId())) {
catalogFromDB = catalogDAO.save(catalog);
} else {
catalogFromDB = null;
}
return mapper.map(catalogFromDB, CatalogDTO.class);
}
public void deleteCatalog(Long id) {
Catalog catalog = catalogDAO.findById(id);
if(catalog != null) {
catalogDAO.delete(catalog.getId());
}
}
}
ProductServiceImpl.java
#Service
public class ProductServiceImpl implements ProductService {
#Autowired
private ProductDAO productDAO;
#Autowired
private ModelMapper mapper;
public ProductDTO findById(Long id) {
Product product = productDAO.findById(id);
return mapper.map(product, ProductDTO.class);
}
public ProductDTO findByName(String name) {
Product product = productDAO.findByName(name);
return mapper.map(product, ProductDTO.class);
}
public ProductDTO findByCategory(Category category) {
Product product = productDAO.findByCategory(category);
return mapper.map(product, ProductDTO.class);
}
public List<ProductDTO> findByUserId(Long id) {
List<Product> products = productDAO.findByUserId(id);
Type listProductsType = new TypeToken<List<ProductDTO>>() {}.getType();
return mapper.map(products, listProductsType);
}
public List<ProductDTO> findAll() {
List<Product> products = productDAO.findAll();
Type listProductsType = new TypeToken<List<ProductDTO>>() {}.getType();
return mapper.map(products, listProductsType);
}
public ProductDTO createProduct(ProductDTO productDTO) {
Product product = mapper.map(productDTO, Product.class);
Product productFromDB = productDAO.save(product);
return mapper.map(productFromDB, ProductDTO.class);
}
public ProductDTO updateProduct(ProductDTO productDTO) {
Product product = mapper.map(productDTO, Product.class);
Product productFromDB;
if(productDAO.exists(product.getId())) {
System.out.println(product.toString());
productFromDB = productDAO.save(product);
} else {
productFromDB = null;
}
return mapper.map(productFromDB, ProductDTO.class);
}
public void deleteProduct(Long id) {
Product product = productDAO.findById(id);
if(product != null) {
productDAO.delete(product.getId());
}
}
}
Now, when I try performing the operations of deletion of catalog or product an error of constraint key fail is triggered. For example trying to delete a product which has a reference in the catalog_product join table:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`e01`.`catalog_product`, CONSTRAINT `FKdx5j7bcx77t7h0hjw6tvoxmp1` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`))
I don't understand if there's a way to set the relations between entities to make what I want in an automatic way with Spring, or if I have to remove records with reference manually before the deletion of the catalog/product.
Thanks a lot in advance to everyone!
Luca

Related

Query to sort data based on another entity

I'm writing a site of a hospital. This is an MVC application with database. Database contains data about patients, doctors etc.
I need to get List of doctors which should be sorted by patient count. I have already tried to do this with Comparator inside Java code like this example:
Page<Doctor> pageDoctor = doctorRepository.findAll(pageable);
List<Doctor> doctorList = pageDoctor.getContent();
doctorList.sort(Comparator.comparing(o -> patientRepository.findAllByDoctor(o).size()));
but I need the sorted list inside Page content. I don't really understand how to make the query equivalent to this example, because I'm new to SQL. Here are my entity and repository classes.
Doctor.java
#Entity
#Table(name = "doctors")
public class Doctor {
#Id
#Column(name = "id", nullable = false, unique = true)
#SequenceGenerator(name="doctors_generator", sequenceName = "doctors_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "doctors_generator")
private Long id;
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "user_id")
private User user;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "doctors_type_id")
private DoctorsType doctorsType;
public Doctor(User user, DoctorsType doctorsType) {
this.user = user;
this.doctorsType = doctorsType;
}
public Doctor() {
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public DoctorsType getDoctorsType() {
return doctorsType;
}
public void setDoctorsType(DoctorsType doctorsType) {
this.doctorsType = doctorsType;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Patient.java
#Entity
#Table(name = "patients")
public class Patient {
#Id
#Column(name = "id", nullable = false, unique = true)
#SequenceGenerator(name="patients_generator", sequenceName = "patients_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "patients_generator")
private Long id;
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "user_id")
private User user;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "doctor_id")
private Doctor doctor;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "treatment_id")
private Treatment treatment;
public Patient(User user, Doctor doctor, Treatment treatment) {
this.user = user;
this.doctor = doctor;
this.treatment = treatment;
}
public Patient() {
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
public Treatment getTreatment() {
return treatment;
}
public void setTreatment(Treatment treatment) {
this.treatment = treatment;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
PatientRepository.java
#Repository
public interface PatientRepository extends JpaRepository<Patient, Long> {
Patient findPatientByUser(User user);
List<Patient> findAllByDoctor(Doctor doctor);
Patient findPatientById(long id);
Page<Patient> findAllByOrderByIdAsc(Pageable pageable);
List<Patient> findAllByOrderByIdAsc();
}
DoctorRepository.java
#Repository
public interface DoctorRepository extends JpaRepository<Doctor, Long> {
Doctor findDoctorById(long id);
Doctor findDoctorByUser(User user);
#Query(
//sql query there
)
Page<Doctor> findAllByPatientCountAsc(Pageable pageable);
Page<Doctor> findAll(Pageable pageable);
List<Doctor> findAllByOrderByIdAsc();
List<Doctor> findAllByDoctorsTypeNot(DoctorsType doctorsType);
List<Doctor> findAllByDoctorsType(DoctorsType doctorsType);
}
Thanks for your answers in advance.
Check this one:
SELECT d FROM Doctor d,
LEFT JOIN Patient p ON d.id = p.doctor.id
GROUP BY d
ORDER BY COUNT(p.id)

Hibernate Map ID automatically from field

I have something similar to this:
#Entity
#Table(name = "claim", schema = "test")
public class Claim implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "idClaim", unique = true, nullable = false)
private Integer idClaim;
#OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JsonManagedReference
private ClaimReturnInfo claimReturnInfo;
#Column(name = "notes")
private String notes;
// Getters and setters
}
#Entity
#Table(name = "claim_returninfo", schema = "test")
public class ClaimReturnInfo implements Serializable {
#Id
#Column(name = "Claim_idClaim")
private Integer id;
#MapsId("Claim_idClaim")
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Claim_idClaim")
#JsonBackReference
private Claim claim;
#Column(name = "description")
private String description;
// Getters and setters
}
ClaimReturnInfo Id is not autogenerated because we want to propagate the Id from its parent (Claim). We are not able to do this automatically and we are getting this error: ids for this class must be manually assigned before calling save() when 'cascade' is executed in ClaimReturnInfo .
Is it possible to map Claim Id into ClaimReturnInfo Id or should we do this manually?
Even if we set this ID manually on claimReturnInfo and we can perform updates, we still get this error when trying to create a new Claim:
// POST -> claimRepository.save() -> Error
{
"notes": "Some test notes on a new claim",
"claimReturnInfo": {
"description": "Test description for a new claimReturnInfo"
}
}
In the ServiceImplemetation:
#Override
#Transactional
public Claim save(Claim claim) throws Exception {
if(null != claim.getClaimReturnInfo()) {
claim.getClaimReturnInfo().setId(claim.getIdClaim());
}
Claim claimSaved = claimRepository.save(claim);
return claimSaved;
}
I have tried using the following mappings and from your comments it was apparent that Json object is populated correctly.
I have noticed that the annotation #MapsId is the culprit.If you check the documentation of #MapsId annotation it says
Blockquote
The name of the attribute within the composite key
* to which the relationship attribute corresponds. If not
* supplied, the relationship maps the entity's primary
* key
Blockquote
If you change #MapsId("Claim_idClaim") to #MapsId it will start persisting your entities.
import javax.persistence.*;
#Entity
#Table(name = "CLAIM")
public class Claim {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "idClaim", unique = true, nullable = false)
private Long idClaim;
#Column(name = "notes")
private String notes;
#OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, optional = false)
private ClaimReturnInfo claimReturnInfo;
public Long getIdClaim() {
return idClaim;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ClaimReturnInfo getClaimReturnInfo() {
return claimReturnInfo;
}
public void setClaimReturnInfo(ClaimReturnInfo claimReturnInfo) {
if (claimReturnInfo == null) {
if (this.claimReturnInfo != null) {
this.claimReturnInfo.setClaim(null);
}
} else {
claimReturnInfo.setClaim(this);
}
this.claimReturnInfo = claimReturnInfo;
}
}
package com.hiber.hiberelations;
import javax.persistence.*;
#Entity
#Table(name = "CLAIM_RETURN_INFO")
public class ClaimReturnInfo {
#Id
#Column(name = "Claim_idClaim")
private Long childId;
#Column(name = "DESCRIPTION")
private String description;
#MapsId
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Claim_idClaim")
private Claim claim;
public Long getChildId() {
return childId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Claim getClaim() {
return this.claim;
}
public void setClaim(Claim claim) {
this.claim = claim;
}
}

Spring Boot - Pagination for Many-to-Many Relationship with extra Column

I can't find a clean and simple way to do pagination when using a many-to-many relationship with an extra column.
My model looks like that:
I have a user and a product model. Each user can consume n products. Each consumption will be stored in an extra table, because I want to store extra information such as date etc. I have implemented the model as follows and it works, but I want to get the consumptions of an user as a Pageable rather than retrieving the whole set. What would be the best way to implement that ?
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(
mappedBy = "user",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Consumption> consumptionList = new ArrayList<>(); // never set this attribute
public List<Consumption> getConsumptionList() {
return consumptionList;
}
public void addConsumption(Product product) {
Consumption consumption = new Consumption(this, product);
consumptionList.add(consumption);
product.getConsumptionList().add(consumption);
}
public void removeConsumption(Consumption consumption) {
consumption.getProduct().getConsumptionList().remove(consumption);
consumptionList.remove(consumption);
consumption.setUser(null);
consumption.setProduct(null);
}
}
--
#Entity
#NaturalIdCache
#org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(
mappedBy = "product",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Consumption> consumptionList = new ArrayList<>();
public List<Consumption> getConsumptionList() {
return consumptionList;
}
}
This is my class to store consumptions.
#Entity
public class Consumption {
#EmbeddedId
private UserProductId id;
#ManyToOne(fetch = FetchType.LAZY)
#MapsId("userId")
private User user;
#ManyToOne(fetch = FetchType.LAZY)
#MapsId("productId")
private Product product;
public Consumption(User user, Product product) {
this.user = user;
this.product = product;
this.id = new UserProductId(user.getId(), product.getId());
}
}
And this is my composite Primary Key which.
#Embeddable
public class UserProductId implements Serializable {
#Column(name = "user_id")
private Long userId;
#Column(name = "product_id")
private Long productId;
private UserProductId() {
}
public UserProductId(Long userId, Long productId) {
this.userId = userId;
this.productId = productId;
}
}
I would like to be able to call a method such as "getConsumptionList(Page page)" which then returns a Pageable.
I hope you can help me!
Thank you in advance!
Well, if using Spring Boot you could use a Repository:
#Repository
public interface ConsumptionRepo extends JpaRepository<Consumption, Long>{
List<Consumption> findByUser(User user, Pageable pageable);
}
Then you can simply call it
ConsumptionRepo.findByUser(user, PageRequest.of(page, size);
I finally found a solution for my problem thanks to #mtshaikh idea:
Just implement a Paginationservice:
public Page<Consumption> getConsumptionListPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Consumption> list;
if (consumptionList.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, consumptionList.size());
list = consumptionList.subList(startItem, toIndex);
}
return new PageImpl<>(list, PageRequest.of(currentPage, pageSize), consumptionList.size());
}

hibernate: deleting entity in a one to many relationship

I have a following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`spindledb`.`section`, CONSTRAINT `FK_ftoru9cp83n512p9is8x3vo53` FOREIGN KEY (`scenario_id`) REFERENCES `scenario` (`scenario_id`))
Here are my classes:
Scenario:
#Entity
#Table(name = "scenario")
public class Scenario {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "scenario_id")
private int id;
#Column(name = "title", nullable = false)
private String title;
#NotNull
#DateTimeFormat(pattern = "dd/MM/yyyy")
#Column(name = "creation_date", nullable = false)
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate creationDate;
#ManyToOne
#LazyCollection(LazyCollectionOption.FALSE)
#JoinColumn(name = "id", nullable = false)
private User user;
#OneToMany(mappedBy = "scenario", orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
private Set<Plot> plotList = new HashSet<Plot>();
#OneToMany(mappedBy = "scenario", orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
private Set<Character> characterList = new HashSet<Character>();
#OneToMany(mappedBy = "scenario", cascade=CascadeType.ALL, orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
#OrderBy("sequence ASC")
private Set<Section> sectionList = new HashSet<Section>();
Section:
#Entity
#Table(name = "section")
public class Section {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "section_id")
private int id;
#Size(min = 4, max = 50)
#NotNull
#Column(name = "name")
private String name;
#NotNull
#Column(name = "type")
private String type = SectionType.TEXT.getSectionType();
#Column(name = "visibility")
private boolean visibility;
#NotNull
#Column(name = "sequence")
private int sequence;
#ManyToOne (cascade=CascadeType.ALL)
#LazyCollection(LazyCollectionOption.FALSE)
#JoinColumn(name = "scenario_id", nullable = false)
private Scenario scenario;
Controller:
#RequestMapping(value = { "/delete-{id}-scenario" }, method = RequestMethod.GET)
public String deleteScenario(#PathVariable int id) {
scenarioService.deleteScenarioById(id);
return "redirect:/home";
}
Scenario service:
#Service("scenarioService")
#Transactional
public class ScenarioServiceImpl implements ScenarioService {
#Autowired
private ScenarioDao dao;
#Override
public Scenario findById(int id) {
return dao.findById(id);
}
#Override
public void saveScenario(Scenario scenario) {
dao.saveScenario(scenario);
}
public void updateScenario(Scenario scenario) {
Scenario entity = dao.findById(scenario.getId());
if(entity!=null){
entity.setTitle(scenario.getTitle());
entity.setCreationDate(scenario.getCreationDate());
}
}
#Override
public void deleteScenarioById(int id) {
dao.deleteScenarioById(id);
}
Dao
#Repository("scenarioDao")
public class ScenarioDaoImpl extends AbstractDao<Integer, Scenario> implements ScenarioDao {
#Override
public Scenario findById(int id) {
return getByKey(id);
}
#Override
public void saveScenario(Scenario scenario) {
persist(scenario);
}
#Override
public void deleteScenarioById(int id) {
Query query = getSession().createSQLQuery("delete from scenario where id = :id");
query.setString("id", ""+id);
query.executeUpdate();
}
I understand that the problem is that there may be a Section that can not exist without scenario. Right now however section table in database is empty and I still can't remove Scenario. Thanks for advice
Deleting an entity via Query would bypass any Cascade settings you put via annotation.
I would suggest find the entity first by id, then delete the entity object:
Object scenario = session.load(Scenario.class, id);
if (scenario != null) {
session.delete(scenario);
}
use cascade=CascadeType.ALL with all #ManyToOne relations in class Scenario because if you are going to delete any Scenario from database it must not be referenced any where in data base.
the other way to delete is.
Serializable id = new Long(1); //your id
Object persistentInstance = session.load(Scenario.class, id);
if (persistentInstance != null) {
session.delete(persistentInstance);
}

JPQL manyToMany unidirectional query

I have been looking the forums and everywhere for a unidirectional manyToMany query. I find many examples but i can't really adjust them to my needs :(
I have 2 entity classes (Anime & User)
Anime {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "anime_id")
private Integer id;
//Other fields etc.
}
User {
#Id
#ValidUsername
#Column(name = "user_id")
private String username;
#ManyToMany()
#JoinTable(name = "users_animes",
joinColumns = #JoinColumn(name = "anime_id", referencedColumnName = "anime_id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "user_id"))
private final List<Anime> animes = new ArrayList<>();
}
Anime simply holds the data from the anime.
User holds the username etc of the user and a list of anime that he subscribed to.
Now i'm trying to find a query that will let me get all the animes in that list.
It's mapped in a table as "users_animes".
Would be a huge help since i'm fairly new to JPQL.
Thanks!
Here's a simple example. Let's assume that we have Country Entity, which can have multiple Citizens:
#Entity
#Table (name = "countries")
public class Country implements Serializable {
#Id
#Column (name = "coun_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column (name = "country_name")
private String countryName;
#ManyToMany (cascade = CascadeType.ALL)
#JoinTable (
name = "citizen_country",
joinColumns = #JoinColumn (name = "country_id", referencedColumnName = "coun_id"),
inverseJoinColumns = #JoinColumn (name = "citizen_id", referencedColumnName = "cit_id")
)
private List<Citizen> citizens;
public Country() {}
public Country(String countryName) {
this.countryName = countryName;
}
//getters and setters
}
And Citizen, which can belong to multiple Countries:
#Entity
#Table (name = "citizens")
public class Citizen implements Serializable {
#Id
#Column (name = "cit_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column (name = "country_name")
private String citizenName;
public Citizen() {}
public Citizen(String citizenName) {
this.citizenName = citizenName;
}
public void setId(Long id) { this.id = id; }
public Long getId() { return id; }
public void setCitizenName(String citizenName) { this.citizenName = citizenName; }
public String getCitizenName() { return citizenName; }
//getters and setters
}
It's unidirectional, just as you wanted it to be. Therefore, Citizen Entity is unaware of Country, so you can't get directly information to which Country a certain Citizen belongs. However, you can retrieve info about which Citizens belong to a certain Country.
Going further, you can populate your tables:
Citizen citizen1 = new Citizen("Smith");
Citizen citizen2 = new Citizen("Kowalski");
Citizen citizen3 = new Citizen("Muller");
dataAccess.saveCitizen(citizen1);
dataAccess.saveCitizen(citizen2);
dataAccess.saveCitizen(citizen3);
// now let's fetch them from DB, along with their other properties (only id in this case)
citizen1 = dataAccess.getCitizenByName("Smith");
citizen2 = dataAccess.getCitizenByName("Kowalski");
citizen3 = dataAccess.getCitizenByName("Muller");
Country country1 = new Country("Foo");
Country country2 = new Country("Bar");
// create lists to hold citizens for each country
List<Citizen> citizenList1 = new ArrayList();
List<Citizen> citizenList2 = new ArrayList();
// add elements to the lists
citizenList1.add(citizen1);
citizenList1.add(citizen2);
citizenList2.add(citizen2);
citizenList2.add(citizen3);
//assign lists of citizens to each country
country1.setCitizens(citizenList1);
country2.setCitizens(citizenList2);
//save data in DB
dataAccess.saveCountry(country1);
dataAccess.saveCountry(country2);
//fetch list of all persisted countries (associated Citizens will come along)
countries = dataAccess.getAllCountries();
And finally:
#Stateless
public class DataAccess {
#PersistenceContext
EntityManager em;
public void saveCountry(Country country) {
em.persist(country);
}
public void saveCitizen(Citizen citizen) {
em.persist(citizen);
}
public Citizen getCitizenByName(String name) {
Query query = em.createQuery("SELECT c FROM Citizen c WHERE c.citizenName = :name");
query.setParameter("name", name);
return (Citizen) query.getSingleResult();
}
public List<Country> getAllCountries() {
Query query = em.createQuery("SELECT c FROM Country c");
return (List<Country>) query.getResultList();
}
}

Categories