0

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.

I want to be able to retrieve all entries which match a certain column, accountRef.

In short, I'd like to execute the following query:

SELECT * from table where accountRef='xyz'

where 'xyz' would be a user input.

The issue I'm facing is is that using @Entity doesn't allow me to not specify IDs. Is there a way I can get around that? Here is my code 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);
}

What I've tried

I tried using @Embeddable but it gives me this error:

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:

MarkJ
  • 411
  • 1
  • 9
  • 23
  • Possible duplicate of [JPA entity without id](https://stackoverflow.com/questions/3820897/jpa-entity-without-id) – Alan Hay Jun 25 '19 at 07:44
  • JPA is not an option without an ID. There is no workaround. If you cannot some kind of unique identifier then fall back to JDBC possibly using mybatis. – Alan Hay Jun 25 '19 at 07:45

2 Answers2

0

You must use either simple primary key or composit key in JPA, and PK is always recommended, without ORM too. 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. Mapstuct is a good choise to make it easy.

zlaval
  • 1,941
  • 1
  • 10
  • 11
0

You need an ID to create the entity. It's as simple as that. However, you can just create an auto increment ID and ignore it. You may not need it but it is needed for 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. 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.

This is an answer that implements a UUID that works using hibernate 4 and spring.

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. 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.

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.

C. Smith
  • 172
  • 1
  • 4
  • 16
  • how woudl I create an auto increment id? i don't have an id column in my database adn can not change the table – MarkJ Jun 25 '19 at 15:11
  • @MarkJ If you can't change the table you can set all the members of the entity as an ID. See [here](https://www.objectdb.com/java/jpa/entity/id#Composite_Primary_Key_) for an example on how to do that. – C. Smith Jun 25 '19 at 19:40
  • but if i have duplicate entries, setting all of the values to @id will still not work becuase entries aren't unique – MarkJ Jun 25 '19 at 21:36
  • I believe JPA recognizes the fact that setting all values to the @ID means that there is not unique values set in your table and will recognize each one as it's own object. You'll just have to be careful calling find one by id since your id are non unique and will throw an unexpected result size exception. – C. Smith Jun 25 '19 at 21:47
  • I tired doing that and I'm still not getting the correct type of data, Is there any other class I can use? – MarkJ Jun 25 '19 at 21:52