本文介绍了如何在JPA父实体中将子关联属性用作Map键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有两个实体 Car 和 CarDescription 其中 CarDescription 取决于表语言中的另一个外键。 我试图完成的是在 Car 中有一个 HashMap ,这样每当我有一个 Car entity-object我可以访问语言ID中的所有描述。 实体 Car.java @Entity @Table(name =Car) public class Car extends AbstractTimestampEntity implements Serializable { private static final long serialVersionUID = -5041816842632017838L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name =ID,unique = true,nullable = false) private Long ID; @OneToMany(mappedBy =car) @MapKeyColumn(name =language_ID) // @MapKey(name =language)//不起作用私人地图< Long,CarDescription> carDescription = new HashMap<>(0); 实体 CarDescription.java @Entity @Table(name =car_description, uniqueConstraints = { @UniqueConstraint(columnNames = {language_id,name})} ) public class CarDescription extends AbstractTimestampEntity implements Serializable { private static final long serialVersionUID = 2840651722666001938L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name =ID,unique = true,nullable = false) private Long ID; @NotNull @ManyToOne 私家车; @NotNull @OneToOne 私有语言语言; // .. } 实体 Language.java @Entity public class语言实现Serializable { private static final long serialVersionUID = 3968717758435500381L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name =ID)私人长ID; // .. } 我的问题是这个映射给了我一张从每个 CarDescription.id 到 CarDescription 的地图。 如何才能完成正确的映射? 解决方案 在 CarDescription 您需要添加 languageId 属性: @Column(name =language_id,insertable = false,updatable = false) private Long languageId; @NotNull @OneToOne @JoinColumn(name =language_id) private语言语言; public void setLanguage(Language language){ this.languageId = language.getId(); this.language = language; } 然后你可以在 Car 这样的实体: @OneToMany(mappedBy =car) @MapKey(name =languageId)私人地图< Long,CarDescription> carDescription = new HashMap<>(0); I'm having two entities Car and CarDescription where CarDescription is depending on another foreign key from the table Language.What I' trying to accomplish is to have a HashMap in Car such that whenever I'm having a Car entity-object I am able to access all descriptions from the language id.Entity Car.java@Entity@Table(name = "Car")public class Car extends AbstractTimestampEntity implements Serializable { private static final long serialVersionUID = -5041816842632017838L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Long id; @OneToMany(mappedBy="car") @MapKeyColumn(name = "language_ID") // @MapKey(name = "language") // does not work either private Map<Long, CarDescription> carDescription = new HashMap<>(0);}Entity CarDescription.java@Entity@Table( name="car_description", uniqueConstraints = { @UniqueConstraint(columnNames={"language_id", "name"}) })public class CarDescription extends AbstractTimestampEntity implements Serializable { private static final long serialVersionUID = 2840651722666001938L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "ID", unique = true, nullable = false) private Long id; @NotNull @ManyToOne private Car car; @NotNull @OneToOne private Language language; // ..}Entity Language.java@Entitypublic class Language implements Serializable { private static final long serialVersionUID = 3968717758435500381L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="ID") private Long id; // ..}The problem I am having is that the mapping gives me a map from each CarDescription.id to CarDescription.How can I accomplish a correct mapping? 解决方案 In CarDescription you need to add the languageId property:@Column(name = "language_id", insertable = false, updatable = false)private Long languageId;@NotNull@OneToOne@JoinColumn(name = "language_id")private Language language;public void setLanguage(Language language) { this.languageId = language.getId(); this.language = language;}Then you can use it in the Car entity like this:@OneToMany(mappedBy="car")@MapKey(name = "languageId")private Map<Long, CarDescription> carDescription = new HashMap<>(0); 这篇关于如何在JPA父实体中将子关联属性用作Map键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 08:50