JPA 2.0 Hibernate @OneToMany + @MapKeyJoinColumn [英] JPA 2.0 Hibernate @OneToMany + @MapKeyJoinColumn
问题描述
OneToMany + MapKeyJoinColumn不适用于我,请提出我做错了什么。
我使用JPA 2.0 + Hibernate 3.6.1
并且想映射下表:
到类:
@实体
公共类问题{
/ / id和其他字段
@OneToMany(mappedBy =question,cascade = CascadeType.ALL)
@MapKeyJoinColumn(name =language_id)
private Map< Language,语句> statement =
new HashMap< Language,Statement>();
}
@实体
公共类语句{
@Id
私有长ID;
@ManyToOne
@JoinColumn(name =language_id,nullable = true)
private语言语言;
@ManyToOne
@JoinColumn(name =question_id,nullable = false)
private问题问题;
@Column(name =message)
private String message;
}
@Entity
公共类语言{
@Id
私有长ID;
@Column(name =name)
私有字符串名称;
}
但它不起作用。 EntityManager持久保存它,但是当我检索Question时,在它的语句映射中只有一个语言到空的条目。
请帮忙
Edit1 :奇怪,但是当我预载所有语言,如下所示:
String sql =从语言l中选择l;
List languages = entityManager.createQuery(sql,Language.class).getResultList();
然后它就可以工作了!
是否有人知道如何让hibernate自动加载某些类的所有对象?
实际上,您的类Statement中的id属性必须是复合主键。您的表语句中不需要statement_id。主键由language_id和question_id组成。
尝试:
@Entity
public class Statement实现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问题产品;
@ManyToOne
私人语言语言;
不要忘记问题中的fetchType以避免
LazyInitializationExceptions
public class Question {
@OneToMany(mappedBy =id。问题,fetch = FetchType.EAGER)
@MapKeyJoinColumn(name =language_id)
私人地图< Language,Statement> statements = new HashMap< Language,Statement>();
}
希望这会有所帮助。
OneToMany + MapKeyJoinColumn doesn't work for me, please suggest what I'm doing wrong.
I'm using JPA 2.0 + Hibernate 3.6.1 And want to map following tables:
To Classes:
@Entity
public class Question {
// id and other fields
@OneToMany(mappedBy="question", cascade = CascadeType.ALL)
@MapKeyJoinColumn(name="language_id")
private Map<Language, Statement> statements =
new HashMap<Language, Statement>();
}
@Entity
public class Statement {
@Id
private Long id;
@ManyToOne
@JoinColumn(name = "language_id", nullable = true)
private Language language;
@ManyToOne
@JoinColumn(name = "question_id", nullable = false)
private Question question;
@Column(name = "message")
private String message;
}
@Entity
public class Language {
@Id
private Long id;
@Column(name = "name")
private String name;
}
But it doesn't work. EntityManager persists it correctly, but when I retrieve Question, in it's statements map there is only one language-to-null entry. Please, help
Edit1: Strange, but when I preload all Languages, like this:
String sql = "select l from Language l";
List languages = entityManager.createQuery(sql, Language.class).getResultList();
then it works!
Does anybody know how to make hibernate automatically load all objects of certain class?
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.
这篇关于JPA 2.0 Hibernate @OneToMany + @MapKeyJoinColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!