Spring Boot and H2 in memory database | Java Techie
ฝัง
- เผยแพร่เมื่อ 15 ต.ค. 2024
- This video Explain how to use H2 in memory database in spring boot application with example
GitHub:
github.com/Jav...
Blogs:
javagyanmantra...
Facebook Group :
/ 919464521471923
Like & Subscribe
Nice Explanation.. by following instruction I able to create REST CRUD spring boot application successfully. Thank you so much
You didn't show us working with h2 database?
Hi sir, can you help me with h2 db connection? II am strucked at memdb not found and I literally tried everything on internet and stack overflow.
Sir, i am getting empty list when i use get method. I follow the same steps as you told in the video. I used the post method to inject values then ,when i use get method i am getting all curly braces with out any data{ {} {} {} } like this. I tried with restarting the server but still having the same problem.could you please help me to solve this problem.
You need to configure Lombok in your IDE
@@Javatechie how to do that?
What IDE you are using
@@Javatechie thanks, now my problem resolved. I forgotted to add getters and setters..
@@Javatechie i am using spring tool suite 4
I have a question for you, why you have added spring dev tools in pom file ?
I added it to enable live auto reload changes
Hi sir, can you connect with me please, little ugent, I need to setup h2 in my spring boot project but its showing driver not existing, I troubleshooted in many ways.
all ok, no error showing, but whenever I am running the project as SpringBoot app, it's showing 61 common frames omitted. Also not strong the data in Postman..
Showing Error: connect ECONNREFUSED (port)
Bro I follow the same procedure but entity and Data some more annotation they were import Jakarta . persistence will be imported my project. So I check my h2 database table is not created ... Why bro please guide me😢
You are using spring boot 3 , in spring boot 3 there is a pkg changes javax pkg renamed as Jakarta .
For your issue it says table not created so just configure Lombok in your ide it will solve your issue
Whenever I include H2 database dependency there is an error in pom.xml file regarding H2 dependency ,why is it so?
What is the error?
All my code is same, dependencies are same. But when I created objects in postman, creation is successfull. But in getall employees url, it is giving empty objects. Please solve my issue
Please configure Lombok in your ide
@java-techie Hi, I have written everything same only, but while sending post request on postman, I m getting error 404
Please check the proper url
@@Javatechie yes I m giving correct url only and the port number is also correct
@@Javatechie i have inserted records using post request, I m getting details saved, but while executing get request, it is giving 500 internal server error
Can you please share your error message in javatechie4u@gmail.com
@@Javatechie hi, actually I created the constructor in the employee entity class. When I removed that, it worked... So what was the issue with the constructor??
Is it possible to save the data in a file in system storage so that when the application gets restarted the previously stored data doesn't get lost?
Yes we can
@@Javatechie it would be very kind of you if you can show us a demo through a simple project like this one.
@@Javatechie yaa bring this video where we would save h2 database
how much ram memory are you using ,yours is so fast mine is very slow
16 GB Ram
nice explanation thank you
Hello I am getting error like un used variables in employee.java file so I am not getting the mapping statement when I run my project kindly help me
Unable to Reach the Mapping Methods, Mapping Methods is not showing in Console. Kindly pls help.
Is this error you are getting ?
@@Javatechie no , you got mapped in console as shown in video but i didnt got.
@@Javatechie bro, i followed the video as it is shown but im not getting data in postman console just getting {}{}{}..please help me to solve this
Did you configure Lombok in your ide if not please configure it or just manually add getter and setter method in your pojo
@@Javatechie yeah brother, added getter and setter and i got it thanks.
Very useful content to start with.
I checked in /h2-console, Employee table is not not auto generating. what i have to do to get table auto generate ?
added below property in application.properties, then started, now working fine
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
Only url is enough to go
@@Javatechie Please pin your comment at the top (just the URL worked for me :)...
Good start
good video..but im getting 403...also tried to check whether table has created or not h2-console itself not opening
Check wrongly you might added spring security starter dependency
getting below error
Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'repository': Error creating bean with name 'employeeRepository' defined in com.myProject.myApp.dao.EmployeeRepository defined in @EnableJpaRepositories declared on MyAppApplication: Not a managed type: class com.myProject.myApp.model.Employee
Fastest project ever seen😮
Great video, I have an issue, the DB it's creating the repository records, but with null data:
{
"id": 1,
"name": null,
"dept": null,
"salary": 0.0
},
I'm using intellyJ, spring 2.5.0
I did what's described in video step by step, could you please give an advise?
Please configure Lombok in your IDE . Go to plugins and add Lombok then re-start your IDE
@@Javatechie It worked..Thanks a lot!!!
help me i have a issue I can't import javax. Presistance. Entity instead of Jakarta Persistence
Request method 'GET' is not supported error how to solve
Please check the http method type that you have mentioned in the controller and what you are hitting from Post man . Both must be the same
@@Javatechie now it will fine. I have another Problem it will store only null values in the h2 database table and how to solve this problem
Please configure Lombok in your ide
@@Javatechie yes bro I configured. Now everything is fine bro thank you
guys don't forget to create getters and setters
Dude here also I have the same question how do u make sure data is getting stored in H2 and data is getting fetched from H2
Saroj please check in pom which dependency we added
Data is inserting.. but while get method it's showing nothing but it's not giving error it just showing [{}]
Did you configure Lombok in your IDE , if not please do that and adding dependency is not enough to play with Lombok
@@Javatechie thanks for your answer. i watched entire video and do changes but i got empty [{}] output for get operation. it is due to not configuring Lombok in my ide
Yes , do one thing if you are beginner manually add getter and setter method in entity and try
Which IDE? Pls share link with version
Intellij idea 💡
Cannot find method - repository.findByDept(dept)
Create this method in your repository classes
@@Javatechie Thanks isssue fixed
Vedio is good I am not able to see H2 database
How the data is storing without configuring data source in props or yml file
Good question , this is an in memory DB so it's not mandatory to configure datasource for this .as you added h2 dependency spring boot is smart enough to create default datasource .
i can't see the table in the database.
Please help
Do you add setter and getter ?
@@Javatechie
yes. already.
Share me your request and dto class
javatechie4u@gmail.com
Okay 😊
How to write one to one relationship mapping in jpa spring boot
I already covered one to many please check that and do change for one to one
@@Javatechie Thanks u quick response..😀😀 .. appreciate it
where is H2 database saving the objects
In memory .
Can someone please help me how to check the h2 database?
Just type localhost:port/h2-console
Thanks helpful
i am getting error
024-04-25 21:22:30.215 ERROR 20672 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Cannot load configuration class: com.javatechie.h2.api.SpringBootH2Application
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:414) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:254) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:128) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at com.javatechie.h2.api.SpringBootH2Application.main(SpringBootH2Application.java:10) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.0.3.RELEASE.jar:2.0.3.RELEASE]
Caused by: java.lang.ExceptionInInitializerError: null
at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer(ConfigurationClassEnhancer.java:122) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:110) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:403) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 17 common frames omitted
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @3d680b5a
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:464) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:336) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:174) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:153) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.proxy.Enhancer.(Enhancer.java:73) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 20 common frames omitted
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @3d680b5a
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) ~[na:na]
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[na:na]
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) ~[na:na]
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) ~[na:na]
at org.springframework.cglib.core.ReflectUtils$1.run(ReflectUtils.java:61) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at java.base/java.security.AccessController.doPrivileged(AccessController.java:569) ~[na:na]
at org.springframework.cglib.core.ReflectUtils.(ReflectUtils.java:52) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:243) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 32 common frames omitted
Awesome...
where is h2 console showned
In browser just hit localhost:port/h2-console
great video