How to create a connection to the H2 database using Spring Data?

Asked

Viewed 2,709 times

2

They played in my lap a project that will be done with Spring Framework and want the data persistence to be done with Spring Data.

I’ve never worked with either of them and I’m having a profound difficulty in generating persistence.

I’ve tried this guide, but it doesn’t work: http://spring.io/guides/gs/accessing-data-jpa/

How can I properly configure Spring Data using the H2 database?

In application.properties looked like this:

spring.datasource.url=jdbc:h2:file:~/nomedobanco
spring.datasource.username=login
spring.datasource.password=senha
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

In Pom, I’m using these packages for jpa and Springdata:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

And the mistake I get is this:

2015-04-13 13:26:31.721 INFO 5068 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {4.3.8.Final} 2015-04-13 13:26:31.723 INFO 5068 --- [ main] org.hibernate.cfg.Environment : HHH000206: Hibernate.properties not found 2015-04-13 13:26:31.725 INFO 5068 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode Provider name : javassist 2015-04-13 13:26:31.893 INFO 5068 --- [ main] o.hibernate.Annotations.common.Version HCANN000001: Hibernate Commons Annotations {4.0.5.Final} 2015-04-13 13:26:31.989 INFO 5068 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2dialect 2015-04-13 13:26:32.051 INFO 5068 --- [ main] o.h.h.i.Ast.Astquerytranslatorfactory : HHH000397: Using Astquerytranslatorfactory 2015-04-13 13:26:32.253 INFO 5068 --- [ main] org.hibernate.tool.hbm2ddl.Schemaexport : HHH000227: Running hbm2ddl schema export 2015-04-13 13:26:32.256 INFO 5068 --- [ main] org.hibernate.tool.hbm2ddl.Schemaexport : HHH000230: Schema export complete 2015-04-13 13:26:33.261 WARN 5068 --- [ main] o.s.w.c.s.Genericwebapplicationcontext : Exception encountered During context initialization - cancelling refresh Attempt

org.springframework.Beans.factory.Beancreationexception: Error Creating bean with name 'requestMappingHandlerAdapter' defined in path class Resource [org/springframework/boot/autoconfigure/web/Webmvcautoconfiguration$Enablewebmvcconfiguration.class]: Bean instantiation via Factory method failed; nested Exception is org.springframework.Beaninstantiationexception: Failed to instantiate [org.springframework.web.servlet.mvc.method.Annotation.Requestmappinghandleradapter]: Factory method 'requestMappingHandlerAdapter' threw Exception; nested Exception is org.springframework.Beans.factory.Beancreationexception: Error Creating bean with name 'mvcConversionService' defined in class path Resource [org/springframework/boot/autoconfigure/web/Webmvcautoconfiguration$Enablewebmvcconfiguration.class]: Bean instantiation via Factory method failed; nested Exception is org.springframework.Beaninstantiationexception: Failed to instantiate [org.springframework.format.support.Formattingconversionservice]: Factory method 'mvcConversionService' threw Exception; nested Exception is org.springframework.Beans.factory.Beancreationexception: Error Creating bean with name 'userRepository': Invocation of init method failed; nested Exception is java.lang.Illegalargumentexception: Not an Managed type: class Marlinalarme.Objects.User at org.springframework.Beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(Constructorresolver.java:599) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(Abstractautowirecapablebeanfactory.java:1119) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(Abstractautowirecapablebeanfactory.java:1014) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abstractautowirecapablebeanfactory.java:504) at org.springframework.Beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(Abstractautowirecapablenfactory.java:476) at org.springframework.Beans.factory.support.Abstractbeanfactory$1.getObject(Abstractbeanfactory.java:303) at org.springframework.Beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(Defaultsingletonbeanregistry.java:230) at org.springframework.Beans.factory.support.AbstractBeanFactory.doGetBean(Abstractbeanfactory.java:299) at org.springframework.Beans.factory.support.AbstractBeanFactory.getBean(Abstractbeanfactory.java:194) at org.springframework.Beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(Defaultlistablebeanfactory.java:755) at org.springframework.context.support.Abstractapplicationcontext.finishBeanFactoryInitialization(Abstractapplicationcontext.java:757) at org.springframework.context.support.Abstractapplicationcontext.refresh(Abstractapplicationcontext.java:480) at org.springframework.boot.SpringApplication.refresh(Springapplication.java:686) at org.springframework.boot.SpringApplication.run(Springapplication.java:320) at org.springframework.boot.test.Springapplicationcontextloader.loadContext(Springapplicationcontextloader.java:101) at org.springframework.test.context.Defaultcacheawarecontextloaderdelegate.loadContextInternal(Defaultcacheawarecontextloaderdelegate.java:68) at org.springframework.test.context.Defaultcacheawarecontextloaderdelegate.loadContext(Defaultcacheawarecontextloaderdelegate.java:86) at org.springframework.test.context.Defaulttestcontext.getApplicationContext(Defaulttestcontext.java:72) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(Servlettestexecutionlistener.java:170) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(Servlettestexecutionlistener.java:110) at org.springframework.test.context.Testcontextmanager.prepareTestInstance(Testcontextmanager.java:212) at org.springframework.test.context.junit4.Springjunit4classrunner.createTest(Springjunit4classrunner.java:200) at org.springframework.test.context.junit4.Springjunit4classrunner$1.runReflectiveCall(Springjunit4classrunner.java:259) at org.junit.Internal.runners.model.ReflectiveCallable.run(Reflectivecallable.java:12) at org.springframework.test.context.junit4.Springjunit4classrunner.methodBlock(Springjunit4classrunner.java:261) at org.springframework.test.context.junit4.Springjunit4classrunner.runChild(Springjunit4classrunner.java:219) at org.springframework.test.context.junit4.Springjunit4classrunner.runChild(Springjunit4classrunner.java:83) at org.junit.Runners.Parentrunner$3.run(Parentrunner.java:290) at org.junit.Runners.Parentrunner$1.Chedule(Parentrunner.java:71) at org.junit.Runners.ParentRunner.runChildren(Parentrunner.java:288) at org.junit.Runners.ParentRunner.access$000(Parentrunner.java:58) at org.junit.Runners.Parentrunner$2.evaluate(Parentrunner.java:268) at org.springframework.test.context.junit4.statements.Runbeforetestclasscallbacks.evaluate(Runbeforetestclasscallbacks.java:61) at org.springframework.test.context.junit4.statements.Runaftertestclasscallbacks.evaluate(Runaftertestclasscallbacks.java:68) at org.junit.Runners.ParentRunner.run(Parentrunner.java:363) at org.springframework.test.context.junit4.Springjunit4classrunner.run(Springjunit4classrunner.java:163)

  • Paulo, welcome to [en.so]! I edited your question to try to adapt it more to the format of questions and answers of the site. However, feel free to edit it again if you think you can improve or add more information. In addition, the OS works best with more specific questions. Tutorials are not by themselves the answer to a problem. Could you please post your current Spring Data configuration? What errors are occurring?

  • Thank you, I updated the Post with the data I have.

2 answers

1


The original error is not exactly your configuration. See below:

Not an Managed type: class Marlinalarme.Objects.User

That means your class User is not a JPA entity. Somehow you must be trying to use this as an entity.

Anyway, at least so far there seems to be no configuration problem. Just check this class issue User not to be a JPA entity.

  • I’ve been reading and seen that I have to declare this class as an entity somewhere, but I can’t find how to create a bean or xml. :(

  • Write down the class with @Entity and see if it resolves. Otherwise, you will need to properly configure the annotation @JpaRepositories in its configuration.

  • Already this @Entity in class. rsrsrsrs. I’ll take a look here at how I set this up.

0

I once had a similar problem using H2 and I discovered that it was my mistake, because I had not put an attribute in my object class... the "id" with their proper notes.

@Entity
class Product {

@Id // Eu havia esquecido de colocar o atributo "id" com essas duas anotações
@GeneratedValue(strategy = GenerationType.AUTO)
Long id
@JsonIgnore
@Column(name='LOJA')
String loja
@Column(name='CODIGO')
String codigo
@Column(name='DESCRICAO')
String descricao
}
  • Specify the solution better, the answer is very generic. etc. is not very welcome in an answer, put more practical examples. Vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.