将Hibernate与Spring Boot,Wildfly和Oracle DB一起使用

时间:2019-01-07 16:26:32

标签: oracle hibernate spring-boot wildfly

我试图了解如何编写一种简单的方法来从数据库检索数据而没有成功。 我正在使用Spring Boot 2.1,Hibernate(由Spring Boot提供),Wildfly 14和Oracle 12。

使用standalone.xml上的数据源(由application.properties文件读取)可以与数据库建立连接。我的问题是,如果我尝试使用EntityManager,EntityManagerFactory或Repositories,则它们始终为null,我无法理解为什么。

可能我在配置中缺少某些内容。我的application.properties是:

struct OrderLine: Codable {
    let absUrl: String?
    let restApiUrl : String?
    let description : String?
    let quantity : Int?
    let subscription: Subs?
    let total: Double?

    private enum CodingKeys: String, CodingKey {
        case absUrl, restApiUrl, description, quantity, subscription, total
    }

    init(from decoder:Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.absUrl = try container.decodeIfPresent(String.self, forKey: .absUrl)
        self.restApiUrl = try container.decodeIfPresent(String.self, forKey: .restApiUrl)
        self.description = try container.decodeIfPresent(String.self, forKey: .description)
        self.quantity = try container.decodeIfPresent(Int.self, forKey: .quantity)
        self.total = try container.decodeIfPresent(Double.self, forKey: .total)
        if (try? container.decodeIfPresent(String.self, forKey: .subscription)) == nil {
            self.subscription = try container.decodeIfPresent(Subs.self, forKey: .subscription)
        } else {
            self.subscription = nil
        }
    }
}

我定义了一个像这样的简单实体

# Datasource
spring.datasource.jndi-name=java:/TestDB

# Hibernate
hibernate.dialect: org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql: true

# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.SQL=debug

现在我不知道该怎么办。 我用一些自动装配的变量(例如EntityManagerFactory)创建了一个DAO类,但它们始终为空。

我该如何操纵我的实体?

1 个答案:

答案 0 :(得分:0)

我通过查看this示例来寻找解决方案。