1

I am trying to create a column 'Barcode' which is to be the primary key and of type string.

this is what i am doing :

// for table-one
    @Id
    @Column(name = "BARCODE", nullable=false)
    private String barcode;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "BARCODE")
    private List<Doc_Mvmnt> doc_mvmnt = new ArrayList<>();

and in another where table-one will have one to many mapping

//for table-two
    @ManyToOne
    @JoinColumn(name = "BARCODE", nullable=false)
    public String barcode;

I am recieving the exception : Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on database.Doc_Mvmnt.barcode references an unknown entity: java.lang.String

1 Answer 1

2

I assume your table-one object is in fact your Barcode object and it has an @Entity annotation on it.

Then, your table-two object is your Doc_Mvmnt object. The problem is that you are establishing a @ManyToOne relationship against a String object (which is not an entity, per your error). Instead, change 'String' to 'Barcode'. I also took the liberty to make it a private member, even though public should work too.

It should look like this:

//for table-two
@ManyToOne
@JoinColumn(name = "BARCODE", nullable=false)
private Barcode barcode;
Sign up to request clarification or add additional context in comments.

2 Comments

no , my table1 is doc_master which has 'barcode' column. I was trying to join the two tables using barcode as the key. I want the key to be of type string, so that in the table it becomes varchar(to make it hold both alphabets and numbers)
The error you are getting is because you are referencing an "unknown entity". A String is a not a Hibernate entity. DocMaster object has the barcode as it's ID (aka, primary key). DocMaster is an entity. Hence, what you need to do is, in table-two, replace 'public String barcode' with 'public DocMaster docMaster'. That will resolve your error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.