[@ElementCollection]
定义基本类型或可嵌入类的实例的集合。如果集合要通过集合表进行映射,则必须指定它。
FetchType fetch
(可选)集合是否应该被延迟加载,或者是否必须急于提取。
默认: javax.persistence.FetchType.LAZY
Class targetClass
(可选)作为集合元素类型的基本类或可嵌入类。
注意 :用@ElementCollection 修饰的属性对象的类的属性名称 不能包含 数据库的关键字
否则会报错
可以使用@CollectionTable 和 @AttributeOverrides来重写相关的关联
package sun.rain.amazing.javax.anno.domain.collection;
import lombok.Data;
import javax.persistence.*;
/**
* @author sunRainAmazing
*/
@Data
@Embeddable
public class UserElementCollectionHobby {
private String favor;
private String uuid;
}
package sun.rain.amazing.javax.anno.domain.collection;
import lombok.Data;
import sun.rain.amazing.core.eums.ErrorPropertyMustCancel;
import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author sunRainAmazing
*/
@Entity
@Data
public class UserElementCollectionClass {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
/**
* 注意要想实现这种方式
* UserElementCollectionHobby 必须使用@Embeddable 注解
* 否则 Could not determine type for
*/
@ElementCollection
private Set<UserElementCollectionHobby> hobby = new HashSet<>();
}
/*
CREATE TABLE `user_element_collection_class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `user_element_collection_class_hobby` (
`user_element_collection_class_id` int(11) NOT NULL,
`favor` varchar(255) DEFAULT NULL,
`uuid` varchar(255) DEFAULT NULL,
KEY `FKhg9boc9w8hf3aogywb2tx7vy7` (`user_element_collection_class_id`),
CONSTRAINT `FKhg9boc9w8hf3aogywb2tx7vy7`
FOREIGN KEY (`user_element_collection_class_id`)
REFERENCES `user_element_collection_class` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/
package sun.rain.amazing.javax.anno.domain.collection;
import lombok.Data;
import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author sunRainAmazing
*/
@Entity
@Data
public class UserElementCollectionString {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
@ElementCollection
private List<String> hobbies;
@ElementCollection
private Set<UserElementCollectionHobby> ueHobby = new HashSet<>();
}
/*
CREATE TABLE `user_element_collection_string` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `user_element_collection_string_hobbies` (
`user_element_collection_string_id` int(11) NOT NULL,
`hobbies` varchar(255) DEFAULT NULL,
KEY `FK8y709mrer2ig239u74igtrwt8` (`user_element_collection_string_id`),
CONSTRAINT `FK8y709mrer2ig239u74igtrwt8` FOREIGN KEY
(`user_element_collection_string_id`)
REFERENCES `user_element_collection_string` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `user_element_collection_string_ue_hobby` (
`user_element_collection_string_id` int(11) NOT NULL,
`favor` varchar(255) DEFAULT NULL,
`uuid` varchar(255) DEFAULT NULL,
KEY `FKatx6g7pehxgfaf37kvk7j59ft` (`user_element_collection_string_id`),
CONSTRAINT `FKatx6g7pehxgfaf37kvk7j59ft`
FOREIGN KEY (`user_element_collection_string_id`)
REFERENCES `user_element_collection_string` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/