Hibernate - 一张表包含多个实体?
我有一个图片
:
public class Picture implements java.io.Serializable {
private byte[] picEncoded;
private String Name;
//etc
是否可以将byte[]
移动到另一个类,而无需在数据库中创建物理上分离的表?我需要使用一些继承策略吗?
编辑
单独实体中的Blob:
pojo:
public class PictureBlob implements java.io.Serializable {
private Integer pictureBlobId;
private byte[] blob;
hbm::
<class name="PictureBlob" table="PICTURE">
<id name="pictureBlobId" type="int">
<column length="200" name="PictureID"/>
</id>
<property name="blob" type="byte[]" insert="false" update="false">
<column name="PicEncoded" not-null="false"/>
</property>
</class>
图片:
hbm::
<one-to-one class="PictureBlob" constrained="true" name="pictureBlob" fetch="select"/>
如何插入新图片?
PictureBlob pictureBlob= new PictureBlob();
pictureBlob.setBlob(new byte[]{84,32,22});
Picture p = new Picture();
p.setPictureBlob(pictureBlob);
session.save(p);
插入 blob 值为 null 的记录。
I have a Picture
:
public class Picture implements java.io.Serializable {
private byte[] picEncoded;
private String Name;
//etc
Is it's possible to move byte[]
to another class without creating physically separated table in db? Do i need to use some inheritance strategy?
edit
Blob in separate entity:
pojo:
public class PictureBlob implements java.io.Serializable {
private Integer pictureBlobId;
private byte[] blob;
hbm::
<class name="PictureBlob" table="PICTURE">
<id name="pictureBlobId" type="int">
<column length="200" name="PictureID"/>
</id>
<property name="blob" type="byte[]" insert="false" update="false">
<column name="PicEncoded" not-null="false"/>
</property>
</class>
Picture:
hbm::
<one-to-one class="PictureBlob" constrained="true" name="pictureBlob" fetch="select"/>
How do i insert new pictures?
PictureBlob pictureBlob= new PictureBlob();
pictureBlob.setBlob(new byte[]{84,32,22});
Picture p = new Picture();
p.setPictureBlob(pictureBlob);
session.save(p);
inserts record where blob value is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用组件映射在 Picture 和 PictureBlob 之间创建组合关系。示例:
POJO
另请注意:
请参阅此处了解更多信息。关于获取策略
已编辑。
Use component mapping which creates a composition relation between Picture and PictureBlob. Example:
POJO
Also Note:
See here for more info. on fetching strategies
Edited.
如果您有兴趣使用注释而不是 hbm,您可以查看这些
http://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html,这将完全解决您的目的。
if you interested in using annotations instead of hbm you can take a look at these
http://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html, this will exactly solve your purpose.
我认为你可以使用这样的东西:
这可能需要一些编辑,但想法是这样的:
您有一个
Picture
类。此类具有name
属性和PictureBlob
类型的属性pictureBlob
。component
标记表示组件内部的属性映射到与Picture
相同的表I think you could use something like this:
This might need some edititng, but the idea is this:
You have a
Picture
class. This class has propertyname
and propertypictureBlob
of typePictureBlob
.the
component
tag indicates the properties inside the component are mapped to the same table asPicture