Spring Boot 持久性xml文件等效java配置(使用Sping Boot )

kqqjbcuj  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(162)

我正在处理一个Spring Boot 项目,其中的实体带有@ EntityDecorator,但没有@ IdDecorator,因为它们是视图而不是表。

A类

package com.lma.flad;
    @Entity
      class ClassA{
          private String name;
          private String surName
       }

B类

package com.lma.flad;
    @Entity
      class ClassB{
          @Id
          private Long id
          private String name;
          private String surName
       }

C类

package com.lma.flad;
    @Entity
      class ClassC{
          @ID
          private Long id
          private String name;
          private String surName
       }

persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
             instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="dtmowliance">

        <class>com.lma.flad.ClassB</class>
        <class>com.lma.flad.ClassC</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="hibernate.cache.region.factory_class"
                      value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        </properties>
    </persistence-unit>
</persistence>

在这种情况下,Hibernate将只分析在持久性xml文件中找到的类(类A和类B)
如何在Spring Boot 时使用java配置来执行persistence.xml

mwg9r5ms

mwg9r5ms1#

由于您已经创建了实体类(JPA),因此必须执行以下后续步骤:
1.创建JPA系统信息库
导入:3个实体通用

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

每个实体的存储库

@Repository
public interface ClassARepository extends JpaRepository<ClassA, Long> {

}
@Repository
public interface ClassBRepository extends JpaRepository<ClassB, Long> {

}
@Repository
public interface ClassCRepository extends JpaRepository<ClassC, Long> {

}

1.属性配置:
配置application.properties文件以将数据库连接到JPA代码。

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

    # Enabling H2 Console
    spring.h2.console.enabled=true

    # Custom H2 Console URL
    spring.h2.console.path=/h2-console

    #Turn Statistics on and log SQL stmts

    spring.jpa.show-sql=true
    spring.jpa.properties.hibernate.format_sql=true
    spring.jpa.hibernate.ddl-auto=create-drop

    #If want to see very extensive logging
    spring.jpa.properties.hibernate.generate_statistics=true
    logging.level.org.hibernate.type=trace
    logging.level.org.hibernate.stat=debug

1.测试与数据库和JPA的连接的Sping Boot
导入java.util.可选;
导入org.slf4j.Logger;导入记录器工厂;导入了一个新的框架,它是一个新的框架。导入了一个脚本文件;导入应用程序;导入应用程序;
导入.实体.类A;//相应地改变import .entity.ClassB;//相应地改变导入.实体.类A;//相应地改变import .repository.ClassARepository;//相应地改变import .repository.ClassBRepository;//相应地改变import .repository.ClassCRepository;//进行相应更改
@SpringBootApplication公共类DB2 JPAApplication实现了命令行运行器{

private Logger logger = LoggerFactory.getLogger(this.getClass());

 @Autowired
 ClassARepository classArepository;

 @Autowired
 ClassBRepository classBrepository;

 @Autowired
 ClassCRepository classCrepository;

 public static void main(String[] args) {
     SpringApplication.run(DB2JPAApplication.class, args);
 }

 @Override
 public void run(String... args) throws Exception 
 {       
     Optional<ClassAEntity> classC = repository.findById(2L);

     logger.info("ClassC id 2 -> {}", classC.get());

     Optional<ClassAEntity> classB = repository.findById(2L);

     logger.info("ClassB id 2 -> {}", classB.get());
 }

注意事项:

  • 您的classA没有@ Id。JPA强制要求具有ID列。
  • 更改DB2JPAApplication类中的实体和资料档案库导入

相关问题