gpt4 book ai didi

java - 使用没有 ID 的实体

转载 作者:行者123 更新时间:2023-12-02 03:58:29 25 4
gpt4 key购买 nike

我在 SQL 数据库中有一个表,其中有重复的条目(这意味着我无法创建复合键)并且没有主键。

我希望能够检索与特定列 accountRef 匹配的所有条目。

简而言之,我想执行以下查询:

从 accountRef='xyz' 的表中选择 *

其中“xyz”是用户输入。

我面临的问题是使用 @Entity 不允许我不指定 ID。有什么办法可以解决这个问题吗?这是我的代码BasicAccountAudit.java

@XmlRootElement
@Entity
//@Embeddable
@Table(name = "tb_Account_History", schema="dbo")
public class BasicAccountAudit implements Serializable{


private String accountRef;
private String client;

//getters and setters

BasicAccountAuditRepository.java

@Repository
public interface BasicAccountAuditRepository extends CrudRepository<BasicAccountAudit, Integer> {

List<BasicAccountAudit> findAll();

List<BasicAccountAudit> findByAccountRef(String accountRef);
}

我尝试过的

我尝试使用@Embeddable,但它给了我这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'basicAccountController': Unsatisfied dependency expressed through method 'setBasicAccountDao' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'basicAccountDaoImpl': Unsatisfied dependency expressed through method 'setBasicAccountAuditRepository' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BasicAccountAuditRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type:

最佳答案

在 JPA 中必须使用简单主键或复合键,并且始终建议使用 PK,也不要使用 ORM。如果你想将实体序列化为没有 id 的 XML(@XmlRootElement 注释对我来说是这样),你可以在 id 字段上使用 @XmlTransient 注释,或者更好的方法是创建一个传输对象并仅映射那些需要序列化的属性。 Mapstuct 是一个不错的选择,可以让这一切变得简单。

关于java - 使用没有 ID 的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56744478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com