【问题标题】:How to make 2 unrelated entities (two repository) run in one project at the same time ? Is it possible?如何使 2 个不相关的实体(两个存储库)同时在一个项目中运行?是否可以?
【发布时间】:2018-09-15 22:31:11
【问题描述】:

我正在尝试在一个项目中创建 2 个实体(不相关)。我正在使用 Java springboot 连接到 mysql 数据库。那么我能做的最好的方法是什么?因为我试图制作 2 个存储库,但我只成功了其中的 1 个。另一个给我一个错误。它给了我错误,即使当我在 mysql 工作台中运行查询时它实际上很好

在我的存储库 (AccountRepository.java) 中不起作用,我放了这样的内容:

public interface AccountRepository extends CrudRepository<Accounts, Integer> {

    @Query("SELECT applicationPassword FROM Accounts where applicationName = 'myApp'")
    String findPasswordApp();
}

在实体文件(Accounts.java)中,我放了这样的东西

@Entity
public class Accounts {
    @Id
    private Integer accountID;
    private String applicationName;
    private String applicationPassword; 
    public void setAccountID(Integer accountID) {
        this.accountID = accountID;
    }

public void setApplicationName(String applicationName) {
    this.applicationName = applicationName;
}

public void setApplicationPassword(String applicationPassword) {
    this.applicationPassword = applicationPassword;
}

public Integer getAccountID() {
    return accountID;
}

public String getApplicationName() {
    return applicationName;
}

public String getApplicationPassword() {
    return applicationPassword;
}
}

但它给了我错误。我从这个实体想要的是我可以检查我已经在 mysql 工作台中手动放入的数据库中的值(读取)。

这是我首先在 mysl 工作台中执行的表查询

CREATE TABLE Accounts (
    accountID int,
    applicationName varchar(255),
    applicationPassword varchar(255)
);  

当我成功将数据插入其中时:

INSERT INTO Accounts (accountID, applicationName, applicationPassword)
VALUES ('1', 'myApp', 'password'));

我的代码有什么问题吗?请帮忙

编辑:我试图掩盖这一行的 try catch 'String password = accountRepository.findPasswordApp(); ' 这给了我像'java.lang.NullPointerException'这样的异常

这是我的全部例外: 在 org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:499) 在 org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:402) 在 org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:206) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 在 java.lang.Thread.run(Thread.java:748) '

【问题讨论】:

  • 抛出的异常是什么?
  • 我尝试用 try catch 覆盖它,但异常显示为“java.lang.NullPointerException”。但我不知道为什么。因为我在 mysql 工作台上运行查询并且它工作正常
  • 尝试调试并找出 accountRepositoryreference 是否为空...如果是,请检查 accountRepository 的 bean 接线/依赖注入
  • 请发布有关您的堆栈跟踪的更多信息。

标签: java mysql spring spring-boot


【解决方案1】:

我很确定您的存储库缺少注释或不在正在扫描的包中。我的项目有 10 个存储库,所以这绝对不是问题,@Query 注释中的 HQL 似乎还不错。

没有理由这 2 不能一起工作,所以我猜你的 @Autowired 用于在运行时抛出错误的 repo 字段为 null。您可能只是忘记了注释。您在 AccountRepository 之上没有 @Repository 注释吗?您的主类或配置中的 @ComponentScan 是否没有包含它的包?

如果缺少其中任何一个,那就是您的问题。你能用异常的全文编辑它吗?

【讨论】:

  • 我有另一个类 (mainRepository.java),它没有 @Repository 注释并且工作正常。我刚刚复制了 mainRepository 并将其重命名为 AccountRepository
  • 它在另一个包中吗?在您要自动装配到的类中执行 System.out.println(repoFieldName==null) 并将 @PostConstruct 添加到方法中以查看它是否为空...
  • 我只是在私有 AccountRepository accountRepository 上方添加@Autowired;正如你所说,它工作正常(没有更多错误),我发现该表实际上是自己创建的,所以我不需要在 mysql 工作台中执行新表。所以谢谢你的回答:)
猜你喜欢
  • 2021-04-30
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 2020-02-06
  • 2019-12-22
  • 1970-01-01
相关资源
最近更新 更多