我的代码看起来像这样:
@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)
使用此代码将获得Unable to find column with logical name: sha1 in org.hibernate.mapping.Table(t_file_meta) and its related supertables and secondary tables
关于SecondaryTable
我从wiki:Tables#Multiple_tables学习。
如何定义这个实体? BTW,T_FILE_META_SHA只有级联update
。
EDITED
所以,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代表META的某些领域。但这样可以吗? FileMeta
不可更新。
当我更改Reference的sha1时,是否可以自动加载FileMeta(如果存在)?
@SecondaryTable允许您以一对一的关系将两个表连接在一起,就像您将同一行拆分为两个单独的表一样。
对于一对多关系,您需要使用@OneToMany
关联。
您的映射问题是您指定了T_FILE_META
表中不存在的连接列:
pkJoinColumns = @PrimaryKeyJoinColumn(name = "sha1", referencedColumnName = "sha1")
因此,您需要将两个sha1字段添加到FileMeta
实体:
@Column(name = "sha1")
private String sha1Fk;
@Column(name = "sha1", table = "T_FILE_META_SHA")
private String sha1Pk;