0

Basically I'm querying a table like the following

keywordId | keyword

1             abc 
1             abcd 
2             feg 
2             xyz 
2             tuv

When I pass a query such as:

"FROM keyword Where keywordId = 2"

I get back the following:

2             feg
2             feg
2             feg

Here's the method I'm using

public List<DataModel> selectRecord(String sqlQuery) {  

    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    session.beginTransaction(); 
    Query query = session.createQuery(sqlQuery);
    List<DataModel> data = query.list();

     session.clear();
     session.close();
     return data;

}

Honestly not too sure why this is happening, but it's also occuring in other tables with the same structure may FK's

Insight appreciated! :)

2 Answers 2

1

Main reason for this is that their is no identifier property in your database. Hibernate is not capable of understanding and that

2 > feg

is different from the below one.

2 > xyz

Best practice would be to define a primary key for the table. You can find more about this issue by visiting this link.Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah that was my first thought too but it still didn't work, turns out I edited the table within first editing the Hibernate data model class to include the PK field, doh. Thanks!
0

Even though Hibernate likes primary keys with only one column, you nevertheless can do as you did, but you must be aware the primary key of your table is both columns keywordId and keyword together.

I guess the error is in your mapping. The mapping for the primary key should be like this:

<composite-id>
    <key-property name="keywordId" column="..." type="int" /> 
    <key-property name="keyword" column="..." type="java.lang.String" /> 
</composite-id>

If another table references to this table, its foreign key also has to be both columns.

Another reason for your error is, you have similar tables, and one class for them extends your keyword class.

Comments

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.