简体   繁体   English

使用没有ID的实体

[英]Using Entity without ID

I have a table in the SQL database that has duplicate entries (which means I can not make a Composite Key) and no primary key. 我在SQL数据库中有一个表有重复的条目(这意味着我不能创建一个复合键),没有主键。

I want to be able to retrieve all entries which match a certain column, accountRef. 我希望能够检索与特定列accountRef匹配的所有条目。

In short, I'd like to execute the following query: 简而言之,我想执行以下查询:

SELECT * from table where accountRef='xyz'

where 'xyz' would be a user input. 其中'xyz'将是用户输入。

The issue I'm facing is is that using @Entity doesn't allow me to not specify IDs. 我面临的问题是使用@Entity不允许我不指定ID。 Is there a way I can get around that? 有没有办法解决这个问题? Here is my code BasicAccountAudit.java 这是我的代码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 BasicAccountAuditRepository.java

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

    List<BasicAccountAudit> findAll();

    List<BasicAccountAudit> findByAccountRef(String accountRef);
}

What I've tried 我试过的

I tried using @Embeddable but it gives me this error: 我尝试使用@Embeddable,但它给了我这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'basicAccountController': Unsatisfied dependency expressed through method 'setBasicAccountDao' parameter 0; org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'basicAccountController'的bean时出错:通过方法'setBasicAccountDao'参数0表示不满意的依赖关系; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'basicAccountDaoImpl': Unsatisfied dependency expressed through method 'setBasicAccountAuditRepository' parameter 0; 嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'basicAccountDaoImpl'的bean时出错:通过方法'setBasicAccountAuditRepository'参数0表示不满意的依赖关系; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BasicAccountAuditRepository': Invocation of init method failed; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'BasicAccountAuditRepository'的bean时出错:init方法的调用失败; nested exception is java.lang.IllegalArgumentException: Not a managed type: 嵌套异常是java.lang.IllegalArgumentException:不是托管类型:

You must use either simple primary key or composit key in JPA, and PK is always recommended, without ORM too. 您必须在JPA中使用简单主键或复合键,并且始终建议使用PK,而不使用ORM。 If you want to serialize the entity to XML ( @XmlRootElement annotation suggest that to me) without id, you can use @XmlTransient annotation on id field, or better way is to create a transfer object and map only those properties you need to serialize. 如果要将实体序列化为XML( @XmlRootElement注释建议我没有id),可以在id字段上使用@XmlTransient注释,或者更好的方法是创建传输对象并仅映射需要序列化的那些属性。 Mapstuct is a good choise to make it easy. Mapstuct是一个很容易的选择。

You need an ID to create the entity. 您需要一个ID来创建实体。 It's as simple as that. 就这么简单。 However, you can just create an auto increment ID and ignore it. 但是,您只需创建一个自动增量ID并忽略它。 You may not need it but it is needed for JPA. 您可能不需要它,但JPA需要它。 Another option if you already have an ID defined is setting up a UUID as your ID and have it auto generate as well. 如果您已经定义了ID,则另一个选项是将UUID设置为您的ID,并将其自动生成。 In this case you won't need to worry about what the value is and there is no order to it but provides the ID for the repository and entity. 在这种情况下,您不必担心值是什么,并且没有订单,但提供了存储库和实体的ID。

This is an answer that implements a UUID that works using hibernate 4 and spring. 是一个实现使用hibernate 4和spring工作的UUID的答案。

In regards to your example for getting a list of info based on user input, you can either define a repository or an EntityManager and write the script yourself. 关于根据用户输入获取信息列表的示例,您可以定义存储库或EntityManager并自行编写脚本。 Take in input from the user and add it to the query then execute it, returning the results. 从用户接收输入并将其添加到查询然后执行它,返回结果。 It's fairly simple to do. 这很简单。 You can check here for EntityManager and here for Repository. 您可以在此处查看EntityManager以及此处的Repository。

But the short answer for your question is, make a random primary key id that you don't need even if the data can have duplicates. 但对于您的问题,简短的答案是,即使数据可能有重复项,也要制作一个您不需要的随机主键ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM