简体   繁体   English

我可以创建@Entity的@ElementCollection吗?

[英]Can I create an @ElementCollection of @Entity?

I'm trying to find relevant JPA documentation, but having trouble finding anything that specifies if I am allowed to make an ElementCollection of an Entity. 我正在尝试查找相关的JPA文档,但是找不到任何指定是否允许创建实体的ElementCollection的内容。 I know the typical use is to make an @ElementCollection of @Embeddable, but due to a Hibernate bug I encountered , I need to make my embeddable class into its own entity. 我知道典型的用途是使@Embeddable的@ElementCollection,但是由于遇到Hibernate错误 ,我需要将可嵌入类变成其自己的实体。

I would like to maintain the entity's lifecycle to be controlled by the parent class. 我想保持实体的生命周期由父类控制。 Consequently, I am hoping not to create any DAO/Repository for the new entity. 因此,我希望不要为新实体创建任何DAO /存储库。

Although Hibernate allows this to occur (it properly generates the DDL), I have not actually tested persistence of the parent entity yet. 尽管Hibernate允许发生这种情况(它会正确生成DDL),但我尚未实际测试父实体的持久性。

Additionally, I have not been able to find any way to specify the join column mapping to the new entity. 此外,我还找不到任何方法可以指定连接列映射到新实体。

For example, given: 例如,给定:

public class User {

    @TableGenerator( name="UUIDGenerator", pkColumnValue="user_id", table="uuid_generator", allocationSize=1)
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator="UUIDGenerator")
    @Column(name = "id")
    private Long id;

    /**
     * Address
     */
    @Valid
    @ElementCollection(fetch=FetchType.LAZY)
    @CollectionTable(name="user_address", joinColumns=@JoinColumn(name = "user_id", referencedColumnName = "id"))
    @OrderColumn
    private List<Address> address;

}

and

@Entity
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    /**
     * Multiple street lines allowable
     */
    @NotBlank
    @ElementCollection(targetClass=String.class, fetch=FetchType.LAZY)
    @CollectionTable( joinColumns=@JoinColumn(name = "address_id", referencedColumnName = "id"))
    @OrderColumn
    private List<String> street;

}

it generates the following MySQL tables: 它生成以下MySQL表:

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user_address` (
  `user_id` bigint(20) NOT NULL,
  `address` bigint(20) NOT NULL,
  `address_order` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`address_order`),
  UNIQUE KEY `UK_m09f5sbmw3q9drll2qig9i07q` (`address`),
  KEY `FK_m09f5sbmw3q9drll2qig9i07q` (`address`),
  KEY `FK_kfu0161nvirkey6fwd6orucv7` (`user_id`),
  CONSTRAINT `FK_kfu0161nvirkey6fwd6orucv7` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
  CONSTRAINT `FK_m09f5sbmw3q9drll2qig9i07q` FOREIGN KEY (`address`) REFERENCES `address` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `address` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `address_street` (
  `address_id` bigint(20) NOT NULL,
  `street` varchar(255) DEFAULT NULL,
  `street_order` int(11) NOT NULL,
  PRIMARY KEY (`address_id`,`street_order`),
  KEY `FK_jrcnrclixxqroefuqc7gjhoh` (`address_id`),
  CONSTRAINT `FK_jrcnrclixxqroefuqc7gjhoh` FOREIGN KEY (`address_id`) REFERENCES `address` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I'd like to find a way to change the field name for user_address . 我想找到一种方法来更改user_address的字段名称。 address . address So far, the only way I can figure out how to do this is to make it a @OneToMany mapping and use a @JoinTable() definition. 到目前为止,我能弄清楚如何执行此操作的唯一方法是使其成为@OneToMany映射并使用@JoinTable()定义。 Is there no way from within @ElementCollection? @ElementCollection内部没有办法吗?

You cannot have an ElementCollection containing Entities, these are mutually exclusive concepts in JPA. 您不能具有包含实体的ElementCollection,这是JPA中的互斥概念。

You can try a one-to-many or many-to-many depending on your needs. 您可以根据需要尝试一对多或多对多。 If you set fetching to eager and cascade all it should be fine. 如果将获取设置为热切并级联,那应该没问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM