Hibernate hsql queries have no effect when called from servlet - java

I am having problems using my DAO (Data Access Object) classes on my servlets.
Everything works fine in a simple main method when not deployed on the server, but using the same classes in my servlets doGet methods seems to have no effect on database. I still get the query messages on console, for example:
Hibernate: select something_id.something from something
but nothing returns...
Im using a memory database (jdbc:hsqldb:mem:db1) and EntityManager to insert and retrieve from it (mostly createQuery()) in my DAO classes methods.
Im testing on eclipse localhost server preview. Tried Tomcat and Glassfish also, but they didnt seem to change anything so I returned to the default.
Does anyone know what am i doing wrong or what might be the issue?
I cant provide any specific code right now, because it is in my work computer, but i will in approximately 12 hours if its needed to help solve this issue.
EDIT
Solved my problem. I foolishly executed my database schema creation file with every get request and thats why the queries didnt seem to work :). I'll be more thorough next time!

Related

Java/Spring-boot: Transactional database update problems

I'm currently working on my react skills by building some simple projects. Today I decided to build a todo app and needed to write a quick backend(in spring cause why not lmao). Everything went fine as usual(I follow amigos code's spring boot tutorial 2022 pattern from youtube to design the backend) until I tried testing the API with postman and I realized that everything works fine but the put/update method. In more detail, whenever I call the put method of the API my object gets updated inside the scope but its not persistent in the database.
Source code is hosted on gh: React-todo-backend
I got a rest api put method that calls my service put method to update its members.
(Controller:put->Service:put->Entity.setVariables(variables))
What I tried:
Adding #Transactional annotation above my put method in my service class(used to work I'm not quite sure if new version of spring changed anything.)
I thought of maybe deleting the entity and saving it as new in the database but my id is auto-generated and unique so I'm probably not gonna follow this approach since I don't want to alter the data myself(instead of the user).
Thanks for your time and help in advance :)
(Its also my first time posting on stackoverflow so and I tried following the rules as much as possible but in case I missed anything please let me know and I'll try to fix it as soon as possible.)
Windows 11, JDK 17, Spring 2.7.4, Mysql
I looked your github project and realized that you didn't use save() method of repository to save your ToDo, use todoRepository.save(todoOptional) in TodoService -> putTodo. If you dont save your changes, row will not be updated that you want to update.

Hibernate find by ID slower than find all

So, for starters, i'm working on an application with an angular front end, Java 8 middleware, and Oracle 12c back end. UI sends the request to the java middleware via REST, middleware access oracle via Hibernate.
In my application, I have an entity type A that has many relationships to other entities. I'm experiencing an issue that when do an entity manager .find for an entity A via its ID, queries for some of these entities are taking upwards of 15 seconds. It seems specific to when type A entity has relationships to a specific type B entity, and I've tried lazily and eagerly fetching to no avail. The problem persists regardless of how it's fetched, and I've logged the time down through all layers of the application, and the slow down is specifically when the find by ID is done at the facade level. That said, if I do a find all query for all type A entities, then the query completes in a reasonable amount of time for the number of entities currently in the database (i.e. much less than 15 seconds). I've tried searching for similar cases and reading documentation, but I can't find a valid reason why this would be happening. Hoping someone with more hibernate experience than myself has run into a similar issue, and can point me in the right direction. Will be happy to elaborate further if needed, but not completely sure what is going on under the hood of hibernate that would explain this behavior. Thank you.
[edit] Thanks for the replies, and apologies for the vagueness. There are a lot of things that affected my ability to be more thorough, or get the exact SQL statements, etc. For anyone who may find this in the distant future, the issue was related to a custom annotation for 2 entity C objects in entity A. I have not gotten exactly to the bottom of the issue as of yet, but have confirmed that was the problem and wanted to update here to say that it's not going to be a general Hibernate issue, but rather something more specific to a custom annotation.

Why does ProGuard break the MySQL connection, how do I fix it?

I am developing an application, and I don't want people decompiling it and stealing the code. Everything converts fine in ProGuard, but one problem I have is that when I try use it when it's obfuscated, the MySQL connection fails. This application relies heavily on MySQL for security reasons.
I took a little bit of time to debug, and ran it with a .bat file, and I got this error:
java.sql.SQLException: defaultAuthenticationPlugin 'com.mysql.jdbc.authentication.MysqlNativePasswordPlugin' is not listed in "authenticationPlugins" nor it is one of the built-in plugins.
To make a long story short, the MySQL works when it isn't obfuscated but it doesn't work when it is. Please help!
Thanks in advance,
Luaq
The problem might be happening because
the classes that implement com.mysql.jdbc.AuthenticationPlugin and which will be used for authentication
are being obfuscated, and MySQL cannot find them anymore due to the name changes. Try keeping the classes that MySQL uses and only obfuscate the fields and methods that are not used directly by MySQL in them.

How is possible to set up hibernate programmatically

Sorry, I am curious if is possible (and if it is, how is possible) to set up the database on what is hibernate working from a running web application, well something like to modify properties.xml or hiberanete.xml from a page of the site.
Thank you,
Vlad.

Spring, Hibernate and Ehcache - Wrong entities

I've got a webapp which uses spring+hibernate for my data layer. I'm using 2nd level caching with ehcache as provider. Everything seems to work so far but sometimes we encounter a problem which I can't really figure out atm.
One of my tables is used for labels within the application - every user who logs access this table with his set language. Works for 90% of the time. But sometimes the user gets labels for the wrong language, e.g. instead of german everything turns to italian.
After a logout and login all labels are correct.
Does anyone of you encountered something like this? I'm not sure where to look at: spring+hibernate+ehcache is a solid package or is it not?
Cheers
Spring/Hibernate/EhCache is a solid stack, I'm 99% sure it's a bug in your code.
Any chance the labels are cached statically somewhere in your application?
Make sure equals and hashcode are proprerly implemented in your entity classes, you can find insights on how to do it here
Try to remote debug your server, set breakpoints in your DAOs to check that the data is actually fetched.

Categories