简体   繁体   English

如何在JPA父实体中将子关联属性用作Map键

[英]How to use a child association property as a Map key in JPA parent entity

I'm having two entities Car and CarDescription where CarDescription is depending on another foreign key from the table Language . 我有两个实体CarCarDescription ,其中CarDescription依赖于表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. 我想要实现的是在Car有一个HashMap ,这样每当我有一个Car实体对象时,我就可以从语言id访问所有描述。

Entity Car.java 实体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 实体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 实体Language.java

@Entity
public 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 . 我遇到的问题是映射给我一个从CarDescription.idCarDescription

How can I accomplish a correct mapping? 我怎样才能完成正确的映射?

In CarDescription you need to add the languageId property: CarDescription您需要添加languageId属性:

@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: 然后你可以在Car实体中使用它,如下所示:

@OneToMany(mappedBy="car")
@MapKey(name = "languageId")
private Map<Long, CarDescription> carDescription = new HashMap<>(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取JPA实体的子关联 - How to fetch child association for an JPA entity 子实体中的JPA空父外键 - JPA Null parent foreign key in child entity 如何使用JPA查询来检索父实体和最后一个子实体 - How to use JPA query to retrieve parent and last child entity JPA - 如何将 2 @ManyToOne 映射到同一个父实体 - JPA - How to map 2 @ManyToOne to the same parent Entity 如何返回一个 Map,其中键是实体属性,值是具有 JPA 和 Hibernate 的实体 - How to return a Map where the key is an entity property and the value is the entity with JPA and Hibernate Sprint Data JPA - 使用共享主键保存父实体的子实体 - Sprint Data JPA - save child entity with parent with shared primary key 如何从spring data jpa中的子主键获取父实体? - How to get the parent entity from the child primary key in spring data jpa? 如何将 JPA 规范从子实体转换为父实体 - how to transform JPA specification from child entity to parent entity 我如何在Hibernate中映射多对一关联,其中孩子有一个组合键,其中一部分是父级的主键? - How do I map a many-to-one association in Hibernate where the child has a composite key and a part of that is the primary key in the parent? JPA 问题,使用父实体的主键作为子实体的主键 - JPA Problem, Using Parent Entity's Primary Key for Child's Entity's Primary Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM