I am trying to use the automatic versioning of Hibernate but when the update method f of the Session is called I do not see the version field in the where clause of the query nor is the version incremented in the database. I am doing something fundamentally wrong probably, but what? Is calling getCurrentSession of sesssionFactory an issue?
I have the following entity class:
package dslibweb.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
#Entity
#Table(name = "dsXCT_Recalls")
public class DsXCT_Recalls {
#Id
public String recallId;
public int version;
public String status;
//...... more properties.....
#Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getRecallId() {
return recallId;
}
public void setRecallId(String recallId) {
this.recallId = recallId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
My controller:
package recalls.controller;
#Controller
public class RecallsDataController {
#Autowired
RecallsService recallsManager;
#Autowired
AuthenticationService authService;
private static final Logger logger = Logger.getLogger(RecallsDataController.class);
private static final String SAVE_RECALLS = "MODIFY XCT RECALLS";
RecallsGrid recalls;
#RequestMapping(value = "/showRecallsGrid")
#ResponseBody
public RecallsGrid showRecallsGrid( HttpSession session, HttpServletResponse response) {
recalls = recallsManager.getRecallsDataGrid((String) session.getAttribute("socketToken"), new GridFilters(0, 0, "", "", "", "", ""));
if (recalls.getError() == null || recalls.getError().equals("")) { // no error
return recalls;
} else {
try {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, recalls.getError());
} catch (IOException e) {
e.printStackTrace();
}
return recalls;
}
}
#RequestMapping(value = "/saveRecalls" , method= RequestMethod.POST)
#ResponseBody
public String saveRecalls( HttpSession session, #RequestParam(value="ids[]", required = false) String [] ids, #RequestParam(value="statuses[]", required = false) String [] statuses){
boolean result = authService.validateUserAction((String) session.getAttribute("socketToken"), SAVE_RECALLS);
if(result)
return recallsManager.saveRecalls(ids, statuses, recalls);
else
return "You do not have authority to perform this action.";
}
}
Where I retrieve a collection of DsXCT_Recalls and show them to the user. The collection is stored in the controller. The user then changes status in one or more entities and I call the saveRecalls method of the recallManager which creates a list of only the changed entities (comparing with the collection stored in the controller).
The recallsManager (service layer) is:
package recalls.service.defaultimpl;
#Service("recallManager")
public class HibernateRecallsDataService implements RecallsService {
#Autowired
JsonRpcRequest jsonReq;
#Autowired
JsonRpcSocketWriterReader socketWriterReader;
#Autowired
JsonRpcRequestConstructor reqConstructor;
#Autowired
RecallsDao hibernateRecallsDao;
private static final Logger logger = Logger.getLogger(HibernateRecallsDataService.class);
#Transactional
public RecallsGrid getRecallsDataGrid(String socketToken, GridFilters filters) {
List<DsXCT_Recalls> recalls = hibernateRecallsDao.findRangeOfRecordsFiltered(filters);
return new RecallsGrid(recalls);
}
#Transactional()
public String saveRecalls(String[] ids, String[] statuses, RecallsGrid recalls) {
List<DsXCT_Recalls> recallList = recalls.getRecalls();
List<DsXCT_Recalls> updatedRecallList = new ArrayList<DsXCT_Recalls>();
for (int i = 0; i < ids.length; i++) {
for (DsXCT_Recalls recall : recallList) {
if (recall.recallId.equals(ids[i])) { // recall is found in the list
if (!statuses[i].equals(recall.getStatus())) { // status has changed
recall.setStatus(statuses[i]);
updatedRecallList.add(recall);
}
}
}
}
return hibernateRecallsDao.saveAll(updatedRecallList);
}
}
The saveAll method of my DAO calls one update method of hibernate session by entity changed:
package recalls.dao.hibernate;
#Repository
public class HibernateRecallsDao implements RecallsDao {
#Autowired(required = true)
#Resource(name = "mySessionFactory")
private SessionFactory sessionFactory;
#SuppressWarnings("unchecked")
public List<DsXCT_Recalls> findRangeOfRecordsFiltered(GridFilters filters) {
return sessionFactory.getCurrentSession().createQuery("from DsXCT_Recalls r WHERE SID = 0 ORDER BY Org, Bank, BIC, SetlDate").list();
}
public String saveAll(List<DsXCT_Recalls> recallList){
int count = 0;
for(DsXCT_Recalls recall:recallList){
sessionFactory.getCurrentSession().update(recall);
count++;
}
return count + " recalls were modified.";
}
}
So apparently the #Version must be above the attribute declaration and not above the getter method.. I am sure I saw this somewhere though. So much time wasted :(
Related
Place that is complaining the error:
#Data
public class AluguelValorForm {
#Autowired
private ValorAluguelMultaService valorAluguelMultaService;
#NotNull #NotEmpty
private String idAluguel;
#NotNull
private Double valor;
public AluguelValor converter(AluguelValorRepository aluguelValorRepository, AluguelForm form ) {
Double valorAluguel = valorAluguelMultaService.valorAluguel(form);
return new AluguelValor(idAluguel,valorAluguel);
}
public AluguelValor update(String idAluguel,Double valor) {
AluguelValor aluguelValor = new AluguelValor();
aluguelValor.setId(idAluguel);
aluguelValor.setValor(valor);
return aluguelValor;
}
Repository:
#Repository
public interface AluguelValorRepository extends MongoRepository<AluguelValor, String> {
Aluguel getReferenceById(String id);
}
Place that I call the method in AluguelValorForm:
#PostMapping
//#CacheEvict(value = "listaDeTopicos",allEntries = true)
public void cadastrar(#RequestBody AluguelForm form) {
Optional<Carro> carro = carroRepository.findByPlaca(form.getPlaca_carro());
Optional<Cliente> cliente = clienteRepository.findByCpf(form.getCpf());
if(carro.isPresent() && cliente.isPresent()) {
Aluguel aluguel2 = form.converter(aluguelRepository);
aluguelRepository.save(aluguel2);
Double valorAluguel = valorAluguelMultaService.valorAluguel(form);
AluguelValor aluguelValor = aluguelValorForm.update(aluguel2.getId(), valorAluguel);
aluguelValorRepository.save(aluguelValor);
}
}
Problem solved. Apparently, it's not possible to #Autowired a class that doesn't have any bean, like my RentValue. That's why I got this error.
I scan two QR code and try to get them from My QR Code Android Mobile App and save it with repository.save() in my Local db.
My app send List to Backend but don't insert to db. When I run localhost/8090, i don't get back anything.
In Browser show only this:
-Find Devices
-Device Code
-Device ID
Developer.java
#Entity
public class Developer {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long id = 0;
private String deviceCode;
private String deviceId;
public Developer() {
super();
}
public Developer(String deviceCode, String deviceID)
{
super();
this.deviceCode = deviceCode;
this.deviceId = deviceId;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String deviceCode() {
return deviceCode;
}
public void set DeviceCode(String deviceCode) {
this.deviceCode;
}
public String deviceId() {
return deviceId;
}
public void set DeviceId(String deviceId) {
this.deviceId;
}
}
DeveloperRepository.java
import org.springframework.data.repository.CrudRepository;
public interface DeveloperRepository extends CrudRepository<Developer, Long> {
}
DeveloperController.java
#Controller
public class DevelopersController {
#Autowired
DeveloperRepository repository;
#RequestMapping(value = "/", method = RequestMethod.POST)
#ResponseBody
private String addDevices(Developer deviceCodeAndId) {
System.out.println("xyz!");
if (!repository.exists(deviceCodeAndId.getId())) {
repository.save(deviceCodeAndId);
return "successfully added " + deviceCodeAndId.getId();
}
return deviceCodeAndId.getId();
}
#RequestMapping(value = "/showall",method = RequestMethod.GET)
public String index(Model model) {
model.addAttribute("index",repository.findAll());
return "index";
}
}
deviceCodeAndID is Class from Android App which scanned with app!
index.html
Have you enabled transaction management. Even you use Spring boot data repository. You need to enable transaction management, else by default everything will be read mode. And for read mode there is no need to transaction. But when you do any operation that will change data in DB, you need to perform transaction management.
Use #EnableTransactionManagement on in application class, and #Transactional in DAO or service class
#SpringBootApplication
public class Application {
#Autowired
DeveloperRepository developerRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Here is my Application Class
My Spring boot app has 2 Entities - Document and Card. Card has column dtFrom. Clients have to work with column daysOnDtConfirm (Document.dtConfirm - dtFrom). Annotation #Formula for GET requests works great, but in PUT response returns an old value of daysOnDtConfirm. How return a new value?
#Entity
#Table(name="document")
public class Document extends BaseEntity{
private String name;
#Column(name = "dt_confirm")
#Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
#DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
#JsonFormat(shape = JsonFormat.Shape.STRING)
private LocalDateTime dtConfirm ;
#Column(name = "contragent_name")
private String contragentName;
....
//CARD
#OneToMany(mappedBy="document" , fetch = FetchType.EAGER)
private List<Card> cards = new ArrayList<Card>();
public List<Card> getCards() {
if (this.cards == null) {
this.cards = new ArrayList<Card>();
}
return this.cards;
}
public void setCard(Card card) {
getCards().add(card);
card.setDocument(this);
}
public int getNrOfCards() {
return getCards().size();
}
....
}
And
#Entity
#Table(name="card")
public class Card extends BaseEntity {
#ManyToOne
#JsonIgnore
#JoinColumn(name = "document_id")
private Document document;
private String name;
private double quantity;
#Column(name = "dt_from")
#Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
#DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
#JsonIgnore
private LocalDate dtFrom ;
#Formula("(select IFNULL(DATEDIFF(Document.dt_confirm , dt_from), 0) from
Document where Document.id = document_id )")
private int daysOnDtConfirm;
...
public void setDtFrom(LocalDate dtFrom) {
this.dtFrom = dtFrom;
}
public void setDtFrom(int daysOnDtConfirm) {
if (this.document.getDtConfirm() != null){
LocalDate dateTo = this.document.getDtConfirm().toLocalDate();
this.dtFrom = dateTo.minusDays(daysOnDtConfirm);
}
}
...
}
Service :
#Service
public class DocumentServiceImpl implements DocumentService {
#Autowired
DocumentRepository documentRepository;
#Autowired
CardRepository cardRepository;
...
#Override
#Transactional
public void changeCard(Document document, Card card) {
//IF ID is NULL then isNew==true!!!!
if (card.isNew()){
card.setDocument(document);
card.setDtFrom(card.getDaysOnDtConfirm());
document.setCard(card);
cardRepository.saveAndFlush(card);
}
else{
Card cardEdit = cardRepository.findOne(card.getId());
if (cardEdit != null) {
cardEdit.setDocument(document);
cardEdit.setName(card.getName());
cardEdit.setUnit(card.getUnit());
cardEdit.setQuantity(card.getQuantity());
//cardEdit.setDtFrom(card.getDtFrom());
cardEdit.setDtFrom(card.getDaysOnDtConfirm());
cardEdit.setDescription(card.getDescription());
cardRepository.saveAndFlush(cardEdit);
}
}
#Override
#Transactional
public Document changeDocumentAndCards(Document document) {
Document documentEdit = changeDocument(document);
List<Card> cards = document.getCards();
//check if the same rows in DB and Client, DELETE difference
deleteCardsFromDocument(document);
//if not empty received from client rows then change
if (!cards.isEmpty()) {
for (Card card : cards) {
changeCard(documentEdit, card);
}
}
return documentEdit;
}
...
}
RestController:
#RestController
#RequestMapping("/api/docs")
public class DocController {
#Autowired
DocumentService documentService;
#RequestMapping(value = "",
method = RequestMethod.GET,
produces = {"application/json", "application/xml"})
#ResponseStatus(HttpStatus.OK)
public #ResponseBody
List<Document> getAllDocument(HttpServletRequest request, HttpServletResponse response) {
List<Document> list = new ArrayList<>();
Iterable<Document> documents = this.documentService.getDocumentAll();
documents.forEach(list::add);
return list;
}
....
#RequestMapping(value = "/{id}",
method = RequestMethod.PUT,
consumes = {"application/json", "application/xml"},
produces = {"application/json", "application/xml"})
#ResponseStatus(HttpStatus.OK)
public Document updateDocument(//#ApiParam(value = "The ID of the existing Document resource.", required = true)
#PathVariable("id") Long id,
#RequestBody Document document,
HttpServletRequest request, HttpServletResponse response) {
Document documentEdit = documentService.changeDocumentAndCards(document);
return documentEdit;
}
...
}
The issue seems to come from the changeDocument(Document document) method. The return value of saveAndFlush() call should be assigned back to documentEdit
UPDATE
The issue is that hibernate will not re-calculate the #Formula field after it is updated. It just fetches it from cache.
The only way I managed to get this working on my machine was to refresh the card entity after updating it. For that to work I needed to add an entity manager in the service class.
In your DocumentServiceImpl (actually could be any service class) class add the following:
public class DocumentServiceImpl implements DocumentService {
//...
#PersistenceContext
private EntityManager em;
#Transactional
public void refreshEntity(Object entity) {
em.refresh(entity);
}
Then, you should call this refreshEntity() method after an update, so that hibernate doesn't fetch it from cache.
This way it worked for me. Hope it helps you.
I want to use dropdown and getting the value in drop down from database, dropdown should contain company code for saving purpose & company description for display purpose.
Below is my code:
Bean Class:
package com.ims.master.company.bean;
public class CompanyBean {
private String id;
private String cmpCode;
private String cmpDes;
private String cmpStatus;
private String cmpCreated;
public CompanyBean(String cmpCode, String cmpDes) {
super();
this.cmpCode = cmpCode;
this.cmpDes = cmpDes;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCmpCreated() {
return cmpCreated;
}
public void setCmpCreated(String cmpCreated) {
this.cmpCreated = cmpCreated;
}
public String getCmpCode() {
return cmpCode;
}
public void setCmpCode(String cmpCode) {
this.cmpCode = cmpCode;
}
public String getCmpDes() {
return cmpDes;
}
public void setCmpDes(String cmpDes) {
this.cmpDes = cmpDes;
}
public String getCmpStatus() {
return cmpStatus;
}
public void setCmpStatus(String cmpStatus) {
this.cmpStatus = cmpStatus;
}
}
DAO class:
package com.ims.master.company.DAO;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ims.hibernate.HibernateUtil;
import com.ims.master.company.bean.CompanyBean;
public class CompanyDAO {
SessionFactory factory = HibernateUtil.getFactory();
Session session = factory.openSession();
ArrayList<CompanyBean> recList = new ArrayList<CompanyBean>();
#SuppressWarnings("unchecked")
public ArrayList<CompanyBean> retrieveCmpCode()
{
System.out.println("=====inside DAO======");
Query query = session.createQuery("select b.cmpCode,b.cmpDes from CompanyBean b where b.cmpStatus=:val");
query.setParameter("val", "Y");
recList = (ArrayList<CompanyBean>) query.list();
System.out.println("=====value====="+recList);
return recList;
}
}
Action Class:
package com.ims.master.masterData;
import java.util.ArrayList;
import com.ims.master.company.DAO.CompanyDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class MasterLookUp extends ActionSupport {
ArrayList companyCode;
public String getCompany()
{
CompanyDAO companyCodeValue = new CompanyDAO();
companyCode = companyCodeValue.retrieveCmpCode();
return SUCCESS;
}
public ArrayList getCompanyCode() {
return companyCode;
}
public void setCompanyCode(ArrayList companyCode) {
this.companyCode = companyCode;
}
}
jsp:
<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>
Please suggest me how the value will come in drop down.
Also suggest that how the value in drop down will be displayed selected in edit part.
You can't cast returned value to ArrayList<CompanyBean>, because Hibernate in your case converts the data returned by the query to List<Object[]>. To return List<CompanyBean> you can use another query.
You need to open the Hibernate session to execute the query, and when you have done with it you should close the session. You don't have to close a session only if it's managed by other tool. You can find a detailed explanation how to use Hibernate session in How to display a list of database records (retrieved via Hibernate) to a JSP page in Struts 2, and linked answer.
The query could return List<CompanyBean> if you change the query and property type, so you can assign a value without casting.
public class CompanyDAO {
public List<CompanyBean> retrieveCmpCode() throws Exception
{
System.out.println("=====inside DAO======");
SessionFactory factory = HibernateUtil.getFactory();
Session session = factory.openSession();
List<CompanyBean> recList;
try {
Query query = session.createQuery("from CompanyBean b where b.cmpStatus=:val");
query.setParameter("val", "Y");
recList = query.list();
System.out.println("=====value====="+recList);
return recList;
} finally {
session.close();
}
}
}
Note: #SuppressWarnings("unchecked") no longer needed.
In the JSP you should bind the select tag to the action property that returns a List<CompanyBean>, similar like you have done already.
<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>
The action
public class MasterLookUp extends ActionSupport {
private List<CompanyBean> companyCode;
public List<CompanyBean> getCompanyCode() {
return companyCode;
}
public void setCompanyCode(List<CompanyBean> companyCode) {
this.companyCode = companyCode;
}
private String cmpCode;
public String getCmpCode() {
return cmpCode;
}
public void setCmpCode(String companyCode) {
this.cmpCode = cmpCode;
}
public String getCompany() throws Exception
{
CompanyDAO companyCodeValue = new CompanyDAO();
companyCode = companyCodeValue.retrieveCmpCode();
return SUCCESS;
}
}
Note: to get/set default/selected value of the select tag you should provide cmpCode property.
you can use below code in your JSP
<html:select property ="cmpDes">
<html:optionsCollection name ="cmpDes" />
</html:select>
When above code is added in ur JSP , your dropdown will have the cmp description which is fetched from DB.
And below is site which has perfect example to learn struts-1 and is also related to your question for getting some ideas to go on.
http://www.javabeat.net/struts-html-optionscollection-tag-htmloptionscollection/
I am using JPA, Hibernate and Spring MVC. In the controller class all the methods works greatly. When I test them in the web browser the public String getModuleFormation(long id) method, that returns an object, and it gives me the following error:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
as a root cause, but yesterday I tried it, and it worked without problem in the localhost:45045/GestionModules/detail/xx URL.
What could cause this problem?
My detail.jsp:
<c:if test="${!empty detailModule}">
${detailModule.idModule}
${detailModule.libModule}
</c:if>
POJO Class + JPA :
#Entity
#Table(name="ModuleFormation")
public class ModuleFormation {
private long idModule;
private String libModule;
public ModuleFormation() {
// TODO Auto-generated constructor stub
}
public ModuleFormation(String libModule) {
this.libModule = libModule;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "seqModule")
#SequenceGenerator(name="seqModule", sequenceName = "seqModuleFormation")
#Column(name="idModule")
public long getIdModule() {
return this.idModule;
}
public void setIdModule(long idModule) {
this.idModule = idModule;
}
#Column(name="libModule", nullable=false, length = 100)
public String getLibModule() {
return this.libModule;
}
public void setLibModule(String libModule) {
this.libModule = libModule;
}
}
DAO Class :
#Repository
public class ModuleFormationDAOImpl implements ModuleFormationDAO {
#Autowired
private SessionFactory sessionFactory;
public void ajouterModuleFormation(ModuleFormation module) {
sessionFactory.getCurrentSession().save(module);
}
public void supprimerModuleFormation(long idModule) {
ModuleFormation module = (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
if(module != null)
sessionFactory.getCurrentSession().delete(module);
}
public List<ModuleFormation> listModuleFormation() {
return sessionFactory.getCurrentSession().createQuery("from ModuleFormation")
.list();
}
public ModuleFormation getModuleFormation(long idModule) {
return (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
}
public void majModuleFormation(ModuleFormation module) {
sessionFactory.getCurrentSession().merge(module);
}
}
Service Class :
#Service
public class ModuleFormationServiceImpl implements ModuleFormationService {
#Autowired
private ModuleFormationDAO moduleDao;
#Transactional
public void ajouterModuleFormation(ModuleFormation module) {
moduleDao.ajouterModuleFormation(module);
}
#Transactional
public void supprimerModuleFormation(long idModule) {
moduleDao.supprimerModuleFormation(idModule);
}
#Transactional
public List<ModuleFormation> listModuleFormation() {
return moduleDao.listModuleFormation();
}
#Transactional
public ModuleFormation getModuleFormation(long idModule) {
return moduleDao.getModuleFormation(idModule);
}
#Transactional
public void majModuleFormation(ModuleFormation module) {
moduleDao.majModuleFormation(module);
}
}
Controller Class :
#Controller
public class ModuleFormationController {
#Autowired
private ModuleFormationService moduleService;
#RequestMapping("/module")
public String listModulesFormations(Map<String, Object> map) {
map.put("module", new ModuleFormation());
map.put("moduleList", moduleService.listModuleFormation());
return "module";
}
#RequestMapping(value = "/ajouter", method = RequestMethod.POST )
public String ajouterModuleFormation(#ModelAttribute("module")
ModuleFormation module,BindingResult result) {
moduleService.ajouterModuleFormation(module);
return "redirect:/module";
}
#RequestMapping(value = "/supprimer/{idModule}")
public String supprimerModuleFormation(#PathVariable("idModule")
long idModule) {
moduleService.supprimerModuleFormation(idModule);
return "redirect:/module";
}
#RequestMapping(value= "/detail/{idModule}")
public String getModuleFormation(#PathVariable("idModule")
long idModule,Map<String, Object> map) {
map.put("detailModule", moduleService.getModuleFormation(idModule));
return "/detail";
}
#RequestMapping(value= "/detail/modifier", method = RequestMethod.POST )
public String majModuleFormation(#ModelAttribute("detailModule")
ModuleFormation module, BindingResult result) {
moduleService.majModuleFormation(module);
return "detail/{idModule}";
}
}
The Javadoc on the Hibernate Session#load(Class, Serializable) method says:
Return the persistent instance of the given entity class with the given identifier,
assuming that the instance exists. This method might return a proxied instance that
is initialized on-demand, when a non-identifier method is accessed.
When you access a property on the object in your JSP the session which loaded the object has been closed.
Use Session#get(Class, Serializable) to ensure that you don't load a proxy.
Instead of sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule), have you tried sessionFactory.getCurrentSession().get(ModuleFormation.class, idModule)?