I am trying to join three tables with my model class.Here is my model classes.
Users.java
#Entity
#Table(name = "users")
public class Users implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
public String username;
public String password;
public Integer privid;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "pid")
private Set<Privillages> priviJoin;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "actid")
private Set<Actions> actionJoin;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#Column(name = "username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
#Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Column(name = "privid")
public Integer getPrivid() {
return privid;
}
public void setPrivid(Integer privid) {
this.privid = privid;
}
public Set<Privillages> getPriviJoin() {
return priviJoin;
}
public void setPriviJoin(Set<Privillages> priviJoin) {
this.priviJoin = priviJoin;
}
public Set<Actions> getActionJoin() {
return actionJoin;
}
public void setActionJoin(Set<Actions> actionJoin) {
this.actionJoin = actionJoin;
}
public Users() {
}
}
And Privillages.java,
#Entity
#Table(name = "privillages")
public class Privillages implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Integer id;
#Column(name = "pname")
public String pname;
#ManyToOne(optional = false)
#JoinColumn(name = "pid", referencedColumnName = "privid")
public Users pid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Users getPid() {
return pid;
}
public void setPid(Users pid) {
this.pid = pid;
}
public Privillages(){
}
}
And Actions.java
#Entity
#Table(name = "actions")
public class Actions implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Integer id;
#Column(name = "actname")
public String actname;
#ManyToOne(optional = false)
#JoinColumn(name = "actid", referencedColumnName = "privid")
public Users actid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getActname() {
return actname;
}
public void setActname(String actname) {
this.actname = actname;
}
public Users getActid() {
return actid;
}
public void setActid(Users actid) {
this.actid = actid;
}
public Actions(){
}
}
My repository containing following code,
#Query(value = "SELECT u.*,p.*,a.* FROM users u "
+ "INNER JOIN privillages p ON u.privid = p.pid "
+ "INNER JOIN actions a ON u.privid = a.actid",
nativeQuery=true)
Set<Users> findByUsername();
My controller action is,
#RequestMapping(value = "/joinResult", method = RequestMethod.GET)
public ModelAndView joinResultShow(Model model)
{
model.addAttribute("joinData",userRepo.findByUsername());
ModelAndView viewObj = new ModelAndView("fleethome");
return viewObj;
}
And my view fleethome is like,
<table>
<th> Username </th>
<th> Privillage </th>
<th> Action </th>
<tr th:each="message : ${joinData}">
<td th:text="${message.username}"></td>
<td><span th:each="privi : ${message.priviJoin}"
th:text="${privi.pname}"></span></td>
<td><span th:each="action : ${message.actionJoin}"
th:text="${action.actname}"></span></td>
</tr>
</table>
I am trying to join Privillages and Actions with my main model Users. Users-Privillages have one to many. And also Users - Actions also have one to many. When I joined Users with Privillages it working good. I successfully joined two table.
Now I also need to join Actions class with Users. I am trying to displaying one column from each Model classes. When I implemented the procedure that I follow previously for joining Users-Privillages is not working here, when I added one more table.
I am getting the error like,
There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "message.pname" (fleethome:65)
How can I join the additional one table with my previous join?
You probably can't do that without model entity changes.
If i got you right, you want to get your entity class with multiple related collections initialized from db. But this can't work as is in your case because MultipleBagFetchException: cannot simultaneously fetch multiple bags. It is basically the same problem of multiple collections with fetch = FetchType.EAGER. Easy fix would be to change Collection<Privillages> to Set<Privillages> or same for Actions if you can.
More info
As for Exception evaluating SpringEL expression: "message.pid.username" the actual reason is that you are trying to work with joinData as if it is some database table record, but instead you should work with it like you would with java classes. Because you already got Set<User> joinData from hibernate. Can try something like
<tr th:each="message : ${joinData}">
<td th:text="${message.username}"></td>
<td><span th:each="privi : ${message.priviJoin}"
th:text="${privi.pname}"></span></td>
<td><span th:each="action : ${message.actionJoin}"
th:text="${action.actname}"></span></td>
</tr>
If you want same output like in the image you provided, you can try:
<div th:remove="tag" th:each="message : ${joinData}">
<div th:remove="tag" th:each="privi : ${message.priviJoin}">
<div th:remove="tag" th:each="action : ${message.actionJoin}">
<tr>
<td th:text="${message.username}"></td>
<td th:text="${privi.pname}"></td>
<td th:text="${action.actname}"></td>
</tr>
</div>
</div>
</div>
Related
I'm using a Thymeleaf HTML registration form and simple save/update method to save/update a 'dish' object to a mySQL database. Restaurant Id is a foreign key for the 'dish' but using the below methods it saves as 'null',
I would like to make it so that the Restaurant id of the currently logged in restaurant owner saves automatically when they add a dish.
Is there an uncomplicated way to do this? The closest tutorial I've found on Youtube involves using JSON requests in Postman and I've had issue adapting that to a HTML registration form in the past.
I'm quite new to all of this so any help would be very much appreciated!
See Dish class:
package com.bron.demoJPA.appuser;
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Builder
#ToString(exclude = "reqlist")
public class Dish {
#Id
#SequenceGenerator(name = "dish_sequence", sequenceName = "dish_sequence", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dish_sequence")
#Column(name = "dish_Id")
private Long dishId;
#Column(name = "dish_name")
private String dname;
#Column(name = "dish_description")
private String description;
#Column(name = "dish_price")
private double price;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "Rest_ID", referencedColumnName = "Rest_ID")
private AppUser app;
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(name = "dish_requirment_mapping", joinColumns = #JoinColumn(name = "dish_Id", referencedColumnName = "dish_Id"), inverseJoinColumns = #JoinColumn(name = "Require_ID", referencedColumnName = "Require_ID"))
private List<Requirments> reqlist;
public void addRequirments(Requirments req) {
if (reqlist == null)
reqlist = new ArrayList<>();
reqlist.add(req);
}
}
See AppUser(restaurant owner) Class
#Column(name = "Rest_Password")
private String password;
#Column(name = "Rest_Email_Address")
private String email;
#Enumerated(EnumType.STRING)
private AppUserRole appUserRole;
private Boolean locked = false;
// don't enable user until email verification
private Boolean enabled = false;
public AppUser(String restname, String email, String pass, AppUserRole app) {
this.restaurantName = restname;
this.email = email;
this.password = pass;
this.appUserRole = app;
}
public Collection<? extends GrantedAuthority> getAuthorities() {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(appUserRole.name());
return Collections.singletonList(authority);
}
#Override
public String getUsername() {
return email;
}
#Override
public String getPassword() {
return password;
}
#Override
public boolean isAccountNonExpired() {
return true;
}
#Override
public boolean isAccountNonLocked() {
return !locked;
}
#Override
public boolean isCredentialsNonExpired() {
return true;
}
#Override
public boolean isEnabled() {
return enabled;
}
#OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
#JoinColumn(name = "openingHourID", referencedColumnName = "OpeningHour_ID")
private OpeningHour opening;
}
See Controller class:
package com.bron.demoJPA.conroller;
#Controller
public class DishController {
//display list of employees
#Autowired
private DishService dishService;
#GetMapping("/dish")
public String viewHomePage(Model model) {
model.addAttribute("listDish", dishService.getAllDish());
return "index";
}
#GetMapping("/showNewDishForm")
public String showNewDishForm(Model model) {
// Create model attribute to bind form data
Dish dish = new Dish();
model.addAttribute("dish", dish);
return "new_dish";
}
#PostMapping("/saveDish")
public String saveDish(#ModelAttribute("dish") Dish dish) {
// save dish to database
dishService.saveDish(dish);
return "redirect:/dish";
}
#GetMapping("/showFormForUpdate/{dishId}")
public String showFormForUpdate(#PathVariable(value = "dishId") long dishId, Model model) {
// get dish from service
Dish dish = dishService.getDishByDishId(dishId);
// set dish as model to pre-populate the form data
model.addAttribute("dish", dish);
return "update_dish";
}
}
See Service implementation
package com.bron.demoJPA.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bron.demoJPA.appuser.Dish;
import com.bron.demoJPA.repository.DishRepository;
#Service
public class DishServiceImpl implements DishService {
#Autowired
private DishRepository dishRepository;
#Override
public List<Dish> getAllDish() {
return dishRepository.findAll();
}
#Override
public void saveDish(Dish dish) {
this.dishRepository.save(dish);
}
#Override
public Dish getDishByDishId(long dishId) {
Optional<Dish> optional = dishRepository.findById(dishId);
Dish dish = null;
if (optional.isPresent()) {
dish = optional.get();
} else {
throw new RuntimeException("Dish not found for: " + dishId);
}
return dish;
}
}
See Service class
public interface DishService {
List<Dish> getAllDish();
void saveDish(Dish dish);
Dish getDishByDishId(long dishId);
}
Can you make sure Dish's "app" attribute is being set correctly before trying to save it?
If it's null or it's a brand new instance of AppUser class it makes sense that when trying to match and persist it ends up on null.
Greetings!
I am trying to create a web service that performs basic CRUD operations written using spring boot 2. The select operation works fine, however the insert, delete and update operations have no effect as their query is not getting generated and executed.
I have looked through different examples but I am unable to figure out any issues. The major concern for me is the fact that not even a query is being triggered for insert, delete or update operations.
Student Entity
#Entity
#Table(name = "student")
#JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Student {
#Id
#NotNull
#Column(name = "id")
private int id;
#NotNull
#Column(name = "name")
private String name;
#Column(name = "course")
private String course;
public Student(int id, String name, String course) {
this.id = id;
this.name = name;
this.course = course;
}
public Student(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
#Override
public String toString() {
return "Student{ id=" + id + ", name='" + name + '\'' + ", course='" + course + '\'' + '}';
}
}
StudentDaoImpl
#Repository
#Transactional
public class StudentDaoImpl implements StudentDao {
#Autowired
private EntityManagerFactory entityManagerFactory;
#Override
public List<Student> fetchAllStudents() {
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Student> cq = cb.createQuery(Student.class);
Root<Student> root = cq.from(Student.class);
CriteriaQuery<Student> all = cq.select(root);
List<Student> solution = session.createQuery(all).getResultList();
session.close();
return solution;
}
#Override
public Student deleteStudent(Integer id) {
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
Student student = session.load(Student.class, id);
if (student != null){
session.delete(student);
session.close();
}
return student;
}
#Override
public Student fetchForId(Integer id){
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
Student student = session.load(Student.class, id);
session.close();
return student;
}
#Override
public Student insertStudent(Student student) {
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
session.save(student);
session.close();
return student;
}
#Override
public Student updateStudent(Student student) {
Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
Student studentCheck = session.load(Student.class, student.getId());
if (studentCheck != null) {
session.saveOrUpdate(student);
session.close();
}
return student;
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Edit
Replacing EntityManagerFactory with EntityManager( + Persistent Context Annotation) worked for me. However I still haven't figured why persistence worked for EntityManager.
If it's not strictly important, you can do it using NativeQuery and its executeUpdate API:
String query = "insert into student values(1,?)";
em.createNativeQuery(query)
.setParameter(1, "Tom")
.executeUpdate();
I would like to suggest this repository
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
}
Probably you have to change the id of Student from int to Integer.
And this repository has the methods for retrieving, updating, creating and deleting.
Let's say that you want to use this repository in a Service, you can do that like this:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
#Service
#Transactional(rollbackFor = Exception.class)
public class StudentService {
#Autowired
private StudentRepository studentRepository;
......
}
I have been struggling to loop through my private static List in my HomeController to display in such a way it shows a list of both students and personnel. Below is an example of what my loop should look like.
[Student] Name:John Surname:Greenberg Email: 123#123 Cellphone:123456789 Age: 20
[Personnel] Name:Rose Surname:Marry Email: email#email Cellphone:123456789 WorkerType: Permanent Degree: BED Education
[Student] Name:Chaz Surname:Brown Email: chazz#gmail.com Cellphone:123456789 Age: 30
Please help me loop properly and Below is my ContestantView i tried coding
#model List<Assignment9_u14333393.Models.ContestantViewModel>
<div style="width:100%;height:auto; background-color:brown; padding-top:10px; padding-bottom:10px;">
<h2 style="text-align:center; color:white;">List of Contestants</h2>
</div>
.
<div class="members" >
<table>
#foreach (var temp in Model)
{
<div class="member">
[#temp.MemberType] Name:#temp.Name Surname:#temp.Surname Email: #temp.Email Cellphone:#temp.CellPhone
</div>
}
</table>
</div>
For additional information I also have three models (StudentViewModel, PersonnelViewModel and ContestantViewModel).
ContestantViewModel is my parent class and StudentViewModel and PersonnelViewModel are my classes which have inherited data members and properties for the parent class.
1st Model
public class ContestantViewModel
{
//Data members
private string mName;
private string mSurname;
private string mCellPhone;
private string mEmail;
private string mMemberType;
//Defeaut Constructor
public ContestantViewModel()
{
mName = "NoName";
mSurname = "NoSurname";
mCellPhone = "NoCellNumber";
mEmail = "NoEmail";
mMemberType = "NoMemberType";
}
//Constructor
public ContestantViewModel(string Name, string Surname, string CellPhone, string Email, string MemberType)
{
mName = Name;
mSurname = Surname;
mCellPhone = CellPhone;
mEmail = Email;
mMemberType = MemberType;
}
//Properties
public string Name
{
get { return mName; }
set { mName = value; }
}
public string Surname
{
get { return mSurname; }
set { mSurname = value; }
}
public string CellPhone
{
get { return mCellPhone; }
set { mCellPhone = value; }
}
public string Email
{
get { return mEmail; }
set {mEmail = value; }
}
public string MemberType
{
get; set;
}
}
2rd Model
public class PersonnelViewModel : ContestantViewModel
{
private string mWorkerType;
private string mDegree;
public PersonnelViewModel(string Name, string Surname, string CellPhone, string Email, string MemberType, string WorkerType, string Degree) : base (Name,Surname,CellPhone,Email, MemberType)
{
mWorkerType = WorkerType;
mDegree = Degree;
}
public PersonnelViewModel()
{
mWorkerType = "NoWorkerType";
mDegree = "NoDegree";
}
public string WorkerType
{
get { return mWorkerType;}
set { mWorkerType = value; }
}
public string Degree
{
get { return mDegree; }
set { mDegree = value; }
}
}
3rd Model
public class StudentViewModel : ContestantViewModel
{
//Data members
private int mAge;
//D Constructor
public StudentViewModel()
{
mAge = 0;
}
//Constructor
public StudentViewModel(string Name, string Surname, string CellPhone, string Email, string MemberType, int Age) : base(Name, Surname, CellPhone, Email, MemberType)
{
mAge = Age;
}
//properties
public int Age
{
get { return mAge; } set { mAge = value; }
}
}
and this is my controller
public class HomeController : Controller
{
// list to hold all my new members
private static List<ContestantViewModel> List = new List<ContestantViewModel>();
// GET: Home
public ActionResult Index()
{
return View();
}
// GET: Signup
public ActionResult Signup(string Name, string Surname, string Email, string Cellphone, string MemberType, int Age,string WorkerType,string Degree)
{
StudentViewModel Stundent = new StudentViewModel();
PersonnelViewModel Personnel = new PersonnelViewModel();
if (MemberType == "Student")
{
//creates instance
Stundent.Name = Name;
Stundent.Surname = Surname;
Stundent.Email = Email;
Stundent.CellPhone = Cellphone;
Stundent.MemberType = MemberType;
Stundent.Age = Age;
// Add data to list
List.Add(Stundent);
}
else
{
//creates instance
Personnel.Name = Name;
Personnel.Surname = Surname;
Personnel.Email = Email;
Personnel.CellPhone = Cellphone;
Personnel.MemberType = MemberType;
Personnel.WorkerType = WorkerType;
Personnel.Degree = Degree;
// Add data to list
List.Add(Personnel);
}
return View(List);
}
}
Put your collection into it's own view model instead of trying to reference it with this line:
#model List<Assignment9_u14333393.Models.ContestantViewModel>
Make a new view model, ViewModelCollections with a collection of your ContestantViewModel. Something like
List<ContestantViewModel> ContestantList
(you can also add others, like your students and workers and reuse the model, even if for some purposes some collections are empty)
Then reference that with:
#model ViewModelCollections
Once you have put all your contestants into the collection you are very close in your current view code.
<div class="members">
<table id="contestantListTable">
<tbody>
#* Column Headers *#
<tr class="contestantListHeaders">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
#* Content Rows *#
#foreach (var temp in Model.ContestantList)
{
<tr>
<td>#temp.mName</td>
<td>#temp.mSurName</td>
<td>#temp.mEmail</td>
<td>#temp.mCellPhone</td>
</tr>
}
</tbody>
</table>
</div>
Instead of this saying "Name: Contestant" "LastName: #1" etc etc
You will have a table:
First Name | Last Name | Email etc etc
Contestant | #1 | ...
.
.
.
I am trying to lazily fetch PetDetails entity using JPA. However, I get LazyInitialization Exception. I read many solutions for this exception and came to find solution using JOIN FETCH in JPQL. Also people recommended using Criteria queries. However, I am trying to look for a solution if there is a way I can fetch the PetDetails entity the way I want without using queries directly or depending on Criteria API or without using EAGER FETCH. I might be missing something. I would appreciate for any help or suggestion. Below are the code samples:
1. Controller class:
#Controller
public class PetController {
private static Logger LOGGER = Logger.getLogger(PetController.class);
#Autowired
private PetService petService;
#RequestMapping(value = "/", method = RequestMethod.GET)
public void manageAndDisplayPet() {
PetDetails petDetails = new PetDetails();
petDetails.setName("DOG");
Pet pet = new Pet(petDetails);
// save
petService.savePet(pet);
// retrieve
LOGGER.debug("**********************" + petService.getPet());
LOGGER.debug("**********************" + pet.getPetDetails());
}
}
2. PetService class:
#Service
public class PetService {
#Autowired
private PetDAO petDAO;
#Transactional
public void savePet(Pet pet) {
petDAO.savePet(pet);
}
#Transactional
public Pet getPet() {
return petDAO.getPet();
}
}
3. PetDAO class
#Repository
#EnableTransactionManagement
public class PetDAO {
#PersistenceContext(unitName = "petcontext")
private EntityManager entityManagerFactory;
public void savePet(Pet pet) {
entityManagerFactory.persist(pet);
}
public Pet getPet() {
Pet pet = (Pet) entityManagerFactory.find(Pet.class, 1);
return pet;
}
}
4. Pet Entity:
#Entity
#Table(name = "t_pet")
public class Pet {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#JoinColumn(name = "pet_details")
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private PetDetails petDetails;
public Pet() {
}
public Pet(PetDetails petDetails) {
this.petDetails = petDetails;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public PetDetails getPetDetails() {
return petDetails;
}
public void setPetDetails(PetDetails petDetails) {
this.petDetails = petDetails;
}
#Override
public String toString() {
return "Pet [id=" + id + ", petDetails=" + petDetails + "]";
}
}
5. PetDetails Entity:
#Entity
#Table(name = "pet_details")
public class PetDetails {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "pet_name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return "PetDetails [id=" + id + ", name=" + name + "]";
}
}
Thank you for your help.
Easy thing you can do is to call pet.getPetDetails() inside PetService#getPet. This solution is not very clear, but it will force JPA to fetch entity too. This is solution for your question, but not the good way anyways.
What is the good way?
The good way may depend on your particular usecase:
If you need this details everytime — you should use EAGER_FETCH
If you need it time to time than good solution it to use JPQL with JOIN FETCH
But the best way is to select not entites, but DTOs, which will contain whole information which your application needs and not more. It may be achieved with SELECT NEW expression as described here
I am using Thymeleaf with Spring Boot and MVC. What I have is a form in which certain checkboxes are being populated with an object(q1) added into the model from the controller
What I want to know is that how can I bind the selected checkboxes with my resulting object(surveyData) into a list of (complex) objects using Thymeleaf properly.
Edit
Also this is the first time I'm working on this combination of Spring Boot, Spring MVC, Spring JPA and entirely the first time I have used Thymeleaf. I'm not exactly sure where the problem may be. I did try debugging a lot but was unable to figure out where the problem is exactly. I tried various different combinations to get this working and got various different outcomes(mostly exceptions of course). It would be difficult for me to post all of them here so if you can suggest something I would love to try it out and post its result.
What I have till now: (The th:field for checkbox is obviously not working)
Code:
HTML Snippet:
<form action="#" th:action="#{/controlleraction}" method="post" th:object="${surveyData}">
<table class="table table-striped jambo_table bulk_action" id="table-resp">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="act,iter : ${q1}">
<td>
<input type="checkbox" class="check" th:value="${act.optionId}" th:field="*{keyResponsibilities.optionId.optionId}" />
</td>
</tbody>
</table>
</form>
SurveyData.java
#Entity
#Table(name = "survey_data")
public class SurveyData implements Serializable{
#Id
#Column(name = "some_id")
private Long someId;
#ElementCollection
#CollectionTable(name = "key_responsibilities", joinColumns = #JoinColumn(name = "data_id"))
private List<KeyResponsibilities> keyResponsibilities = new ArrayList<>();
public List<KeyResponsibilities> getKeyResponsibilities() {
return keyResponsibilities;
}
public void setKeyResponsibilities(List<KeyResponsibilities> keyResponsibilities) {
this.keyResponsibilities = keyResponsibilities;
}
}
KeyResponsibilities.java
#Embeddable
public class KeyResponsibilities {
#OneToOne
#JoinColumn(name = "option_id")
private OptionsMaster optionId;
#Column(name = "other")
private String other;
public OptionsMaster getOptionId() {
return optionId;
}
public void setOptionId(OptionsMaster optionId) {
this.optionId = optionId;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
}
OptionsMaster.java
#Entity
#Table(name = "options_master")
public class OptionsMaster implements Comparable<OptionsMaster> {
private int optionId;
private String text;
#Id
#Column(name = "option_id")
public int getOptionId() {
return optionId;
}
public void setOptionId(int optionId) {
this.optionId = optionId;
}
#Basic
#Column(name = "text", length = -1)
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
#Override
public int compareTo(OptionsMaster o) {
return Integer.compare(this.getOptionId(),o.getOptionId());
}
}