gpt4 book ai didi

java - 使用 N :1 relationship 将同一列保存在 JPA secondaryTable 中

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:12 25 4
gpt4 key购买 nike

我的代码如下所示:

@Data
@Accessors(chain = true, fluent = true)
@Entity
@Table(name = "T_FILE_META", indexes = {@Index(columnList = "filename")}
)
@SecondaryTable(name = "T_FILE_META_SHA",
pkJoinColumns = @PrimaryKeyJoinColumn(name = "sha1", referencedColumnName = "sha1")
, indexes = {@Index(columnList = "sha1", unique = true), @Index(columnList = "md5", unique = true)}
)
public class FileMeta
{
@Id
@GeneratedValue
private Long id;

private String filename;

@Column(length = 40, table = "T_FILE_META_SHA")
private String type;
@Column(length = 40, table = "T_FILE_META_SHA")
private String sha1;
@Column(length = 32, table = "T_FILE_META_SHA")
private String md5;
}

我想要两个表:T_FILE_META,T_FILE_META_SHA,关系是N:1

T_FILE_META( pk(id),filename, fk(sha1))
T_FILE_META_SHA( pk(sha1), md5, type)

使用此代码将得到无法在org.hibernate.mapping.Table(t_file_meta)及其相关的超表和辅助表中找到逻辑名称为sha1的列

关于我从wiki:Tables#Multiple_tables学习的SecondaryTable .

如何定义这个实体?顺便说一句,T_FILE_META_SHA 仅级联更新

已编辑

所以,SecondaryTable只是1:1的,我把实体改成了

/**
* Different filename can reference to a same file meta
*/
@Data
@Accessors(chain = true, fluent = true)
@Entity
@Table(name = "T_FILE_REF",
indexes = {@Index(columnList = "filename"), @Index(columnList = "owner")}
)
public class FileReference
{
@Id
@GeneratedValue
private Long id;

private String filename;
private String owner;

/**
* The sha1 reference may change
* <p/>
* This field only used for FK
*/
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private String sha1;
@Column(columnDefinition = "default now()")
private Date lastModificationDate;
@Column(columnDefinition = "default now()")
private Date uploadDate;


@Getter(AccessLevel.NONE)
@ManyToOne(cascade = CascadeType.PERSIST, optional = false)
@JoinColumn(name = "sha1",
referencedColumnName = "sha1",
updatable = false,
nullable = false)
private FileMeta meta;


public FileMeta meta()
{
if (meta == null)
{
meta = new FileMeta();
}
return meta;
}

public String md5()
{
return meta().md5();
}

public String type()
{
return meta().type();
}

public String sha1()
{
return meta().sha1();
}


@Data
@Accessors(chain = true, fluent = true)
@Entity
@Table(name = "T_FILE_META", indexes = {@Index(columnList = "md5")})
public static class FileMeta
{
@Id
@Column(length = 40,
unique = true,
updatable = false,
nullable = false)
private String sha1;

@Column(length = 32, updatable = false, nullable = false)
private String md5;

/**
* File size
*/
@Column(precision = 32, nullable = false)
private Long length;

/**
* Mime type
*/
@Column(length = 40)
private String type;
}
}

这看起来更明智,REF:META 是 N:1。REF 委托(delegate) META 的某些字段。但是这样可以吗? FileMeta 不可更新。

当我更改Reference的sha1时,是否可以自动加载FileMeta(如果存在)?

最佳答案

@SecondaryTable允许您以一对一的关系将两个表连接在一起,就像您将同一行拆分为两个单独的表一样。

对于一对多关系,您需要使用 @OneToMany 关联。

映射的问题在于您指定的连接列在 T_FILE_META 表中不存在:

pkJoinColumns = @PrimaryKeyJoinColumn(name = "sha1", referencedColumnName = "sha1")
  • name:引用 T_FILE_META 列(不存在)
  • referencedColumnName:引用 T_FILE_META_SHA 列

因此,您需要将两个 sha1 字段添加到 FileMeta 实体中:

@Column(name = "sha1")
private String sha1Fk;

@Column(name = "sha1", table = "T_FILE_META_SHA")
private String sha1Pk;

关于java - 使用 N :1 relationship 将同一列保存在 JPA secondaryTable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com