JPA 2.0 Hibernate @OneToMany + @MapKeyJoinColumn

流过昼夜 提交于 2019-12-05 06:27:17

In fact, the id property in your class Statement must be a composite primary key. You do not need a statement_id in your table Statement. The primary key is composed of both language_id and question_id.

Try that :

    @Entity
    public class Statement implements Serializable {

        private static final long serialVersionUID = 1L;

        @EmbeddedId
        private StatementId id = new StatementId();

        @Column(name = "message")
        private String message;
    }

    @Embeddable
    private static class StatementId implements Serializable {

        private static final long serialVersionUID = 1L;

        StatementId(){}

        @ManyToOne
        private Question product;

        @ManyToOne
        private Language language;

    }

And do not forget the fetchType in Question in order to avoid LazyInitializationExceptions.

public class Question {

    @OneToMany(mappedBy="id.question", fetch=FetchType.EAGER)
    @MapKeyJoinColumn(name="language_id")
    private Map<Language, Statement> statements = new HashMap<Language, Statement>();
}

Hope it will help.

Are you using second-level caching? Is your object already part of the session (first-level caching)? Try doing a session clear()

Try calling statements.size() to make sure the data are fetched or you could try defining the fetch type of your relationship to EAGER : @OneToMany(mappedBy="question", cascade = CascadeType.ALL, fetch=FetchType.EAGER)

You might want to read this : http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Eager_Join_Fetching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!