【问题标题】:persistence xml file equivalent java configuration with spring boot持久性xml文件等效java配置与spring boot
【发布时间】:2020-04-04 10:08:35
【问题描述】:

我正在开发一个 Spring Boot 项目,其中我有带有 @Entity 装饰器的实体,它们没有 @Id 装饰器,因为它们是视图而不是表。这就是为什么我有一个persistence.xml 文件,它说类在这里处理休眠

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 文件中找到的类(classA 和 Class B)
我如何在 spring boot 中使用 java config 来做 persistence.xml

【问题讨论】:

  • 如果您使用的是 springboot,您可以使用应用程序属性文件进行配置
  • 是的,我使用的是 springboot,我如何使用应用程序属性文件来做到这一点?
  • 视图与 Hibernate 的实体相同,视图可以有 '@Id' 装饰器。将“@Id”装饰器添加到您认为可用于识别记录的视图字段。在您的示例中,它可以是字段“名称”
  • 记住@ID 对于 JPA 中的每个实体都是必需的 stackoverflow.com/questions/29561885/…

标签: java spring hibernate spring-boot


【解决方案1】:

由于您已经创建了实体类 (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

3.Spring Boot 测试你与数据库和 JPA 的连接

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import <your-path>.entity.ClassA;//change accordingly
import <your-path>.entity.ClassB;//change accordingly
import <your-path>.entity.ClassA;//change accordingly
import <your-path>.repository.ClassARepository;// change accordingly
import <your-path>.repository.ClassBRepository;// change accordingly
import <your-path>.repository.ClassCRepository;// change accordingly

@SpringBootApplication
public class DB2JPAApplication implements CommandLineRunner {

    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 类中的实体和 Reppsitory 导入

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多