简体   繁体   中英

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. 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.

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.

Although Hibernate allows this to occur (it properly generates the DDL), I have not actually tested persistence of the parent entity yet.

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:

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 . 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. Is there no way from within @ElementCollection?

You cannot have an ElementCollection containing Entities, these are mutually exclusive concepts in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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