Null pointer in persistentBag in backing bean - java

I would like to know why I am getting null pointer in my bean method. On my front end I'm interating on value adp.domain.elementsDomain, since the labels are being properly displayed on the screen.
This is my front:
<c:forEach items="#{atributosDaPesquisaBean.atributosDaPesquisaList}" var="adp" varStatus="loop">
<h:panelGrid columns="2" cellpadding="20">
<h:outputText value="#{adp.dominio.nome}"/>
<p:selectManyCheckbox id="gridQueryResult" layout="grid" columns="2">
<f:selectItems value="#{adp.dominio.elementosDominio}" var="elemento" itemLabel="#{elemento.label}" itemValue="#{elemento}" />
<f:ajax listener="#{atributosDaPesquisaBean.incluiNaPesquisa(elemento)}"/>
</p:selectManyCheckbox>
</h:panelGrid>
</c:forEach>
This is my bean:
#ManagedBean
#SessionScoped
public class AtributosDaPesquisaBean {
private List<AtributosDaPesquisa> atributosDaPesquisaList;
#PostConstruct
public void init() {
AtributosDaPesquisaDAO adpDao = new AtributosDaPesquisaDAO();
atributosDaPesquisaList = adpDao.buscaPorRelatorio(relatorio.getCodigo());
atributosDaPesquisaList.forEach(adp -> adp.setaClausulaDeData());
for(AtributosDaPesquisa adp: atributosDaPesquisaList){
if(adp.isEhDominio()){
List<ElementoDominio> elementosDominio = adp.getDominio().getElementosDominio();
elementosDominio.forEach(elemento -> elemento.setEstaNaPesquisa(false));
}
}
}
public void incluiNaPesquisa(ElementoDominio elemento){
//here elemento comes as null. Why?
elemento.setEstaNaPesquisa(true);
}
}
And these are my models:
#Entity
#Table(name = "tbl_atributosdapesquisa")
public class AtributosDaPesquisa {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "atr_codigo")
private Long codigo;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "tbl_dominio_dom_codigo", referencedColumnName = "dom_codigo")
private Dominio dominio;
//others columns
//getters and setters
}
Other model:
#Entity
#Table(name = "tbl_dominio")
public class Dominio {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "dom_codigo")
private Long codigo;
#Column(name="dom_nome", nullable = false)
private String nome;
#OneToMany(fetch = FetchType.EAGER, mappedBy = "dominio")
private List<ElementoDominio> elementosDominio;
//getters and setters
}
And finally:
#Entity
#Table(name = "tbl_elemento_dominio")
public class ElementoDominio {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ele_dom_codigo")
private Long codigo;
#Column(name="ele_dom_label", nullable = false)
private String label;
#Column(name="ele_dom_esta_na_pesquisa", nullable = false)
private boolean estaNaPesquisa;
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "dom_codigo", nullable = false)
private Dominio dominio;
//getters and setters
}
In my method 'incluiNaPesquisa' which is getting null as parameter. I do not know the reason since my screen is rendering the list and showing each of the labels. If I send AtributoDaPesquisa as parameter, I did not receive null pointer, but the object.

Related

how to save entities in a relation to database in spring boot

I have a spring boot application with two entities in a relationship. MeetingSetting and MeetingTime meetingSetting can have unlimited meetingTimes. So far the databases are generating without problem, but When I try to save my Entity they are saved but different from each other, they are saved independently. Meaning MeetingName which is a foreign key inside MeetingTime is not saved but seen as null (I debugged and tried finding out why but could not find anything) THe other values are saved-
could someone point me out what my error is?
this is the json I am sending:
{
"meetingName":"TEst",
"meetingPw":"",
"meetingTime":[
{
"date":"2021-05-31",
"startTime":"15:30",
"endTime":"16:30"
},
{
"date":"2021-06-21",
"startTime":"15:30",
"endTime":"17:30"
},
{
"date":"2021-06-21",
"startTime":"11:01",
"endTime":"11:01"
}
]
}
MeetingSettings:
#Entity
#Table(name = "meeting_settings")
#Data
public class MeetingsSetting {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "meeting_name", unique = true)
private String meetingName;
#Column(name = "meeting_url")
private String meetingUrl;
#Column(name = "meeting_pw")
private String meetingPw;
#OneToMany(mappedBy = "meeting_Name", cascade = CascadeType.ALL)
private Set<MeetingTime> meetingTime = new HashSet<>();
}
MeetingTime:
#Entity
#Table(name = "meeting_times")
#Data
public class MeetingTime {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "meeting_date")
private String date;
#Column(name = "start_time")
private String startTime;
#Column(name = "end_time")
private String endTime;
#ManyToOne
#JoinColumn(name = "meeting_name" ,insertable = false, updatable = false , referencedColumnName = "meeting_name")
private MeetingsSetting meeting_Name;
}
this is how I try to save the entity:
#RestController
#RequestMapping("/api/meetingSetting")
public class MeetingSettingController {
#Autowired
MeetingSettingService meetingSettingService;
#PostMapping("/")
public void saveMeeting(#RequestBody MeetingsSetting meetingsSetting){
meetingSettingService.saveMeeting(meetingsSetting);
}
}
My service calls the save method of an jpaRepository.
In a bi-directional One to Many, you have to synchronize both sides of the association.
You can simply iterate over all MeetingTime objects and set the corresponding MeetingSetting to it.
Your MeetingSettingService's saveMeeting method could do this:
public void saveMeeting(MeetingsSetting meetingsSetting) {
// ...
// here you're synchronizing both sides of the association
meetingsSetting.getMeetingTime()
.forEach(mt -> mt.setMeetingSetting(meetingSetting));
// ...
repository.save(meetingSetting);
}
Solution to my question, I am not sure if this is a good or correct way of solving this maybe someone can advice me a better solution:
#Entity
#Table(name = "meeting_times")
#Data
public class MeetingTime implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "meeting_date")
private String date;
#Column(name = "start_time")
private String startTime;
#Column(name = "meeting_name")
private String meeting_name;
THIS IS THE PART WHICH IS CALLED FROM THE METHOD INSIDE MEETINGSCONTROLLER
#Column(name = "end_time")
private String endTime;
#ManyToOne
#JoinColumn(name = "meeting_name" ,insertable = false, updatable = false, referencedColumnName = "meeting_name")
private MeetingsSetting meetingName;
}
MeetingsTime Entity:
#RestController
#RequestMapping("/api/meetingSetting")
public class MeetingSettingController {
#Autowired
MeetingSettingService meetingSettingService;
#PostMapping("/")
public void saveMeeting(#RequestBody MeetingsSetting meetingsSetting){
meetingsSetting.getMeetingTime()
.forEach(mt -> mt.setMeeting_name(meetingsSetting.getMeetingName()));
// ...
meetingSettingService.saveMeeting(meetingsSetting);
}
}

How to set parent entity in child entity object without constructor and setter method in hibernate?

I have two entities VacationTour and vacationTourDestinations. One tour can have multiple destinations. I want to save tour and destinations in the db using save() method of crudRepository with single save call. I don't want to manually set tour in the destination object. But I need the id of tour in destination as foreign key. Is this feasible?
VacationTourDestination.class
#Entity
#Table(name = "vacationTourDestinations")
public class VacationTourDestination {
#Id
#Column(name = "Id")
#GeneratedValue(strategy = GenerationType.AUTO)
protected int id;
#JoinColumn(name = "TourId")
#ManyToOne(fetch = FetchType.LAZY)
VacationTour tour;
#JoinColumn(name = "Country")
#ManyToOne
private Country country;
#Column(name = "destination")
String destination;
}
VacationTour.class
#Entity
#Table(name = "vacationTours")
public class VacationTour {
#Id
#Column(name = "Id")
#GeneratedValue(strategy = GenerationType.AUTO)
protected int id;
#Column(name = "adult")
int adult;
#Column(name = "child")
int child;
#Column(name = "infant")
int infant;
#Column(name = "termsAndConditions")
String TAC;
#OneToMany(mappedBy = "tour",cascade = CascadeType.ALL,orphanRemoval = true)
protected List<VacationTourDestination> tourDestinations;
}

Referential integrity constraint violation error in JPA

I am trying to parse a web request and save to database. I have 3 models and first node is virtualDocument. This is the uniq table (according to request url). VirtualRequest table has all erquest bodies and HttpHeaderList table has all thhp headers according to their virtualRequest bean id.
when I tried to save the first log I got and error like this;
org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK1TW2G47F7A47580KQVMDJWGBQ: PUBLIC.T_VIRTUAL_REQUEST FOREIGN KEY(REQUEST_ID) REFERENCES PUBLIC.T_VIRTUAL_DOCUMENT(DOCUMENT_ID) (65)"; SQL statement:
insert into t_virtual_request (request_id, media_type, method_type, request_url) values (null, ?, ?, ?) [23506-192]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.192.jar:1.4.192]
here is VirtualDocument bean
#Entity
#Table(name = "t_virtual_document")
public class VirtualDocument {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "document_id")
private long documentId;
#Column(name = "real_url", unique = true)
private String realURL; //uniq
#Column(name = "virtual_url", unique = true)
private String virtualURL; //uniq
#Column(name = "simulation_mode", columnDefinition = "varchar(10) default 'STOP'")
private String simulationMode;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "request_id")
private List<VirtualRequest> requestList;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "response_id")
private List<VirtualResponse> responseList;
//getter setter without any annotation
}
here is VirtualRequest bean;
#Entity
#Table(name = "t_virtual_request")
public class VirtualRequest {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "request_id")
private long requestId;
#Column(name = "request_url")
private String requestURL;
#Column(name = "method_type")
private String methodType;
#Column(name = "media_type")
private String mediaType;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "header_id")
private List<HttpHeaderList> requestHeaders;
//getter setter without any annotation
}
here is HeaderList bean;
#Entity
#Table(name = "t_http_headers")
public class HttpHeaderList {
#Id
#Column(name = "header_id")
private long headerId;
#Column(name = "header_key")
private String headerKey;
#Column(name = "header_value")
private String headerValue;
}
I think this is what you want instead:
#Entity
#Table(name = "t_virtual_document")
public class VirtualDocument {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "document_id")
private long documentId;
#Column(name = "real_url", unique = true)
private String realURL; //uniq
#Column(name = "virtual_url", unique = true)
private String virtualURL; //uniq
#Column(name = "simulation_mode", columnDefinition = "varchar(10) default 'STOP'")
private String simulationMode;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "virtualDocument")
private List<VirtualRequest> requestList;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "virtualDocument")
// Note the mappedBy parameter. This points to the property in the entity that owns the relationship (in this case the VirtualResponse).
private List<VirtualResponse> responseList;
//getter setter without any annotation
}
#Entity
#Table(name = "t_virtual_request")
public class VirtualRequest {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "request_id")
private long requestId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "document_id")
private VirtualDocument virtualDocument;
#Column(name = "request_url")
private String requestURL;
#Column(name = "method_type")
private String methodType;
#Column(name = "media_type")
private String mediaType;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "virtualRequest")
private List<HttpHeaderList> requestHeaders;
//getter setter without any annotation
}
#Entity
#Table(name = "t_http_headers")
public class HttpHeader { /*Note this is a more appropriate name for the entity since it holds the data of a single header.*/
#Id
#Column(name = "header_id")
private long headerId;
#Column(name = "header_key")
private String headerKey;
#Column(name = "header_value")
private String headerValue;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "request_id")
private VirtualRequest virtualRequest
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "response_id")
private VirtualResponse virtualResponse;
}
Updated the answer to add mapping the headers to the request entity.

How to show related objects in a jstl using a spring?

I have 3 objects: User, Comment and StatusUpdate(news). This is the User...
#Entity
#Table(name = "users")
#PasswordMatch(message = "{register.repeatpassword.mismatch}")
public class SiteUser {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Long id;
#Column(name = "email", unique = true)
#Email(message = "{register.email.invalid}")
#NotBlank(message = "{register.email.invalid}")
private String email;
#Transient
#Size(min = 5, max = 15, message = "{register.password.size}")
private String plainPassword;
#Column(name = "password", length = 60)
private String password;
#Column(name = "enabled")
private Boolean enabled = false;
#NotNull
#Column(name = "firstname", length = 20)
#Size(min = 2, max = 20, message = "{register.firstname.size}")
private String firstname;
#NotNull
#Column(name = "surname", length = 25)
#Size(min = 2, max = 25, message = "{register.surname.size}")
private String surname;
#Transient
private String repeatPassword;
#Column(name = "role", length = 20)
private String role;
public SiteUser() {
}
Here comes the StatusUpdate(you can call it piece of news or article). That has a site user that is the one who has created that article.
#Entity
#Table(name = "status_update")
public class StatusUpdate {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Size(min=5, max=255, message="{addstatus.title.size}")
#Column(name = "title")
private String title;
#Size(min=5, max=5000, message="{addstatus.text.size}")
#Column(name = "text")
private String text;
#Column(name = "added")
#Temporal(TemporalType.TIMESTAMP)
#DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss")
private Date added;
#OneToOne(targetEntity = SiteUser.class)
#JoinColumn(name="user_id")
private SiteUser siteUser;
#PrePersist
protected void onCreate() {
if (added == null) {
added = new Date();
}
}
public StatusUpdate() {
}
And the Comment which can be done by any registered user, right? As you will notice the Comment has no User object to avoid circular references.
#Entity
#Table(name = "comments")
public class Comment {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#ManyToOne
#JoinColumn(name = "statusupdateid")
private StatusUpdate statusUpdate;
#Column(name = "commenttext")
private String commenttext;
#Column(name = "commentdate")
#Temporal(TemporalType.TIMESTAMP)
#DateTimeFormat(pattern = "yyyy/MM/dd hh:mm:ss")
private Date commentdate;
#Column(name = "userid")
private Long userid;
public Comment() {
}
Now I would like to show in my JSP an article, with all the related comments and each of them belong to a different user. Can I use a HashMap to relate the users and their comments? I do not see how.
#RequestMapping(value ="/viewonestatus/{id}")
public ModelAndView viewOneStatus(#PathVariable("id") Long id) {
StatusUpdate status = statusUpdateService.get(id);
int countComments = commentService.countStatusComments(status);
List<Comment> comments = commentService.readAllComments(status);
ModelAndView modelAndView = new ModelAndView();
for (Comment comment: comments){
SiteUser user = userService.get(comment.getUserid());
modelAndView.getModel().put("user", user);
}
modelAndView.getModel().put("commentscounter", countComments);
modelAndView.getModel().put("status", status);
modelAndView.getModel().put("comments", comments); //!!
modelAndView.setViewName("app.viewonestatus");
return modelAndView;
}
As you expect, when my JSP shows just one user (the last one) for all the comments, but I can not relate all the Comments with the corresponding Users
<table class="table table-hover">
<c:forEach var="comment" items="${comments}">
<tr>
<td>
<div class="col-sm-2 sm-margin-bottom-40">
<img class="img-responsive profile-img margin-bottom-20" id="profilePhotoImage" src="/profilephoto/${comment.userid}" />
</div>
<h4>
${user.firstname} ${user.surname}
<span>
<!-- <span>${counterUserMap[comment.key]}</span> -->
5 hours ago / Reply
</span>
</h4>
<p>
<fmt:formatDate pattern="EEEE d MMMM y 'at' H:mm:ss" value="${comment.commentdate}" />
</p>
<p>${comment.commenttext}</p>
</td>
</tr>
</c:forEach>
I do not want to use JSON. I'm thinking about an anonymous class with all the stuff inside. Well, I'm open to your thoughts. Thanks.
Shokulei answer was the solution:
Since you have the userid, you can link it using the #ManyToOne annotation. This would be the most ideal way. But if you really don't want to link them, then you can create a new #Transient SiteUser siteUser; attribute in Comment class. And then in your for loop, you can use comment.setSiteUser(user); instead of modelAndView.getModel().put("user", user);. Hope this will help.
Thanks Shokulei

OneToMany PersistentBag

I have a problem with storing OneToMany relationship with Hibernate.
What I got is AdvertisementData entity which looks as follows:
#Entity
#Table(name = "advertisement_data")
public class AdvertisementData {
#Id
#Column(name = "order_id", unique = true, nullable = false)
#GeneratedValue(generator = "gen")
#GenericGenerator(name = "gen", strategy = "foreign", parameters = #Parameter(name = "property", value = "order"))
private Long id;
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE, mappedBy = "advertisementData")
private List<KRPData> krpData;
//setters and getters
}
and KRPData which is defined as follows:
#Entity
public class KRPData {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String description;
#Type(type = "org.hibernate.type.SerializableToBlobType")
private List<String> images;
private String section;
#Type(type = "org.hibernate.type.SerializableToBlobType")
private List<String> filenames;
#ManyToOne
private AdvertisementData advertisementData;
//getters and setters
}
I can see that both entities are stored, however every time I fetch AdvertisementData, the results looks like:
KRPData is returned as PersistentBag without any data.
Hibernate version: 4.2.2.Final
Any ideas what could be the case?
Thank You in advance!

Categories

Resources