Java/Spring-boot: Transactional database update problems - java

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.

Related

Why does SpringBoot API corrupt itself with no obvious reason?

I am working on a boilerplate SpringBoot-React CRUD project as a basis for something bigger that I want to do. The project also uses MSSQL as a database and Spring JPA to interface with it.
UPDATE
Considering another angle - can it have to do with switched corporate VPNs, if the project is stored on a networked drive, or should some of the metadata in the workspace be deleted, and then re-clean and update the project?
UPDATE 2
He's dead, Jim:
I will be still working to figure this out.
What has been frustrating is that after building the API and it working, it gets a spontaneous error that I can't explain, and which forces me to recreate the entire project. In a word, the implementation of the repository gets the "add unimplemented methods" error, when the interface itself has no methods specified. Then, I get an error in the POM file about the "MANIFEST.MF" file missing. This occurs with the API just running without a problem for a time, and then it just breaks. This has occurred a few times already, but now it's getting problematic, because I also don't have a lot of time left for the project.
For context:
Typical MVC project structure:
The implementation error:
Adding the unimplemented methods will result in the addition of every conceivable method in JPA, when none of them are declared in the interface:
The interface had a declared methods that was implemented in the Controller, which was just due to a shortcoming in the example I was basing this on:
I am suspecting that this is an STS4 issue? Coincidentally, it then doesn't stop "Computing...", again spontaneously:
After all of the above happens, the POM file gives me the MANIFEST.MF Error:
There is also an error at the line:
I have tried reinstalling the Eclipse IDE on this, I've ensured that the environment is compatible (Java 11, Spring 2.7.6, Maven, installed libs are JPA, Dev Tools, MSSQL Driver, Web - typical ones for the sort of the project). SQL configuration also worked correctly before, with the connection via the endpoints.
The front end is built in React and uses Axios to marry it to the endpoint, and that was working fine before.
Similar posts exist, but nothing quite like this
Error "MANIFEST.MF already exists in VFS" when creating new artifact
MANIFEST.MF (The system cannot find the path specified)
I really don't want to have to recreate the API again, but I am about out of other options and time.
Thank you in advance!
In order to get the API to start:
First, it looks like the problem had to do with the fact that the project was on an network drive, which caused the initial errors; copying the project to a local C: drive directory got rid of them. It appears that whatever process Maven or Spring Boot uses to update references in this case doesn't capture folder redirection in the context of network drives; it might be because of a different proxy connection, but I can't be sure.
Then, you might an error that the API can't start, because it can't make a bean of the controller type. To resolve that, first check that your PK value is of the same type as the one in your database; in my case that was an integer type, since I am operating with MS SQL.
In my case, I also had to add the UserRepositoryImpl class file back in and implement all the methods from CrudRepository to clear the project of errors. The, do a clean and update, the project starts.
I still have the implementation of my repo methods in the controller, and the connection with the frontend is still broken, but at least I have an operational API again, without having to rebuild it.
Thanks again to everyone who commented!

Where to put #Transactional and the logic which invokes two or more tables?

I am currently developing a web server using Spring Boot.
Since I am a beginner in Spring Boot and server and I have no developer around me,
I am not sure if I am doing all right or all wrong.
My project structure on Spring Boot server is like this.
As you see there are six entities, which are tables in MySQL.
Each of them has its controller, service and repository. (and the repositories implements JpaRepository)
Clover is a lottery name like Powerball.
If a user buys a clover, that clover should be added to a purchased clover list which is CloverValid in the picture, and user's point has to be decreased by 10.
So, when a user clicks a button on a website or an android app,
the server has to update the user's point in User table,
and insert a value of new clover - his id and a timestamp.
And of course those two operations should be transactional.
I want to know where to put this logic.
I think it's service so should I add a new service class or
change the whole structure?
If I do have to change this structure and start again
please tell me how or give a little advice or tip for me.
Thank you and I am really sorry if I am violating any rules of Stack Overflow without knowing it.
A simple structure could be entity driven, in your case UserController/UserService/UserRepository/User.
Transactional boundaries belong to the service layer, you can use Spring annotations and use them on public methods.

Alfresco : Java delegate how can i all details from DB using acvitit Intance Id

Alfresco DB I have created one column called tid whenever user create workflow the tid generate one key , I want get tid from DB using activitiIntance id ,Is this possible ? Please help out with reference
You should never use the database directly using repositories.
Any repository allows you to work using APIs at a application level without considering the specific DBMS behind Alfresco or Activiti.
I suggest you to follow a different approach using Alfresco and Activiti APIs.
Hope this helps.
It might help you to break the problem down into smaller chunks. For example...
First, create a separate database for this. You never want to touch the Alfresco database or tables.
Next, write a standalone Java class that can connect to the database and perform CRUD functions against entries in that database. As you suggest, you could use the activiti process ID as a primary key if you want, but for now you'll use a fake value. This step is not Alfresco or Activiti related at all so there should be plenty of resources online to help you with this part.
Once you are satisfied that is working, move your code into an Activiti task listener that does what you need it to do in the context of your business process. If your code worked without Alfresco and Activiti it should still work, only now you are hooking it up to your process by grabbing the real process ID.
In your other post I gave you a link to a workflow tutorial that explains how to write a custom task listener.

Neo4j Trigger Example Using TransactionEventHandler()

I would like to build out a set of triggers in my database using TransactionEventHandler() functionality.
However I haven't found a working example of this for version > 3.0. I did see an example by maxdemarzi however it doesn't appear to be working in recent versions of neo4j.
If anyone has any experience with this I would really appreciate the help!
Side Note: I do realize APOC has some alpha functinality around triggers using cypher. At the moment it isn't fully fledged and I have run into some issues using it. Thus I'm looking at implementing my own plugin to handle my particular use case.
After reaching out to maxdemarzi on github he has updated his example to support neo4j v 3.1.
See the repo here: https://github.com/maxdemarzi/neo_listens

Hibernate hsql queries have no effect when called from servlet

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!

Categories