![](/img/trans.png)
[英]How to Do a Join using Hibernate between two Entities (unrelated Entities)
[英]How to make 2 unrelated entities (two repository) run in one project at the same time ? Is it possible?
我正在尝试在一个项目中创建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'));
我的代码有什么问题吗? 请帮忙
编辑:我试图掩盖这行尝试捕获'字符串密码= accountRepository.findPasswordApp(); ”,这给了我类似“ java.lang.NullPointerException”的异常
这是我的全部例外情况:在org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:499) 402),位于org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:206),位于java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511),位于java.util.concurrent java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(ScheduledThreadPoolExecutor.java:180)处的.FutureTask.run(FutureTask.java:266)在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor $ ScheduledFutureTask.run)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)在java.lang.Thread.run(Thread.java:748) )'
我很确定您的存储库缺少注释或不在正在扫描的软件包中。 我有10个存储库的项目,所以绝对不是问题,@ Query批注中的HQL似乎还可以。
没有理由认为这2个不能一起工作,所以我猜想您的@Autowired字段的回购引发错误在运行时为null。 您可能只忘记了一个注释。 在AccountRepository顶部是否没有@Repository批注? 您的主类或配置中的@ComponentScan是否没有包含它的软件包?
如果缺少任何这些,那是您的问题。 您可以使用例外全文对其进行编辑吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.