如何在休眠状态下为@ElementCollection设置表名

2022-09-01 10:16:11

我正在使用

public class UserTask extends BaseObject implements Serializable {
    
    @ElementCollection(targetClass = java.lang.String.class) 
    private List<String> userTaskMessage = new ArrayList<>();
   
   .
   .
   .
   .

休眠生成表,其名称为 。usertask_usertaskmessage

如何设置我的姓名?例如my_table_example


答案 1

使用注释@CollectionTable(name=“your_table_name”)


答案 2

JPA:

@Entity
@Table(name = "SERVICES")
public class Service {

    @Id
    @Column(name = "ID") 
    private Long id;

    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "SERVICE_LINKED_CODES", joinColumns = @JoinColumn(name = "SERVICE_ID"))
    @Column(name = "CODE") 
    private List<String> linkedCodes;

}

数据库中的架构:

CREATE TABLE SERVICES { ID BIGINT };
CREATE TABLE SERVICE_LINKED_CODES { SERVICE_ID BIGINT, CODE VARCHAR(255) };

推荐