存储来自休眠数据库中第三方库的对象

时间:2018-06-11 12:04:47

标签: java hibernate

在开始之前,我对hibernate没有多少经验。我正在开发一个应用程序,我使用同事库来显示一些数据。我想在我的数据库中存储记录对象,因此每次使用特定功能时都不需要创建它们。我想从数据库中读取它们。首先我尝试这样做:

@Entity
public class RecordingEntity
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Lob
    @Column(name = "recording")
    private Recording recording;

    @ManyToOne
    @Column(name = "user")
    private User user;

    public RecordingEntity(Recording recording, User user)
    {
        this.recording = recording;
        this.user = user;
    }

    public Recording getRecording()
    {
        return recording;
    }

    public Long getId()
    {
        return id;
    }

    public void setRecording(Recording recording)
    {
        this.recording = recording;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Long getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }
}

public class ImportRecording()
{
        ...
        try
        {
            TapestryDBConnection.performAtomic(new DBAtomicRunnable()
            {
                @Override
                public void run(Session session) throws InterruptedException, IOException, SerialException, SQLException
                {
                    ...
                    RecordingEntity recordingEntity = new RecordingEntity(recording);
                    session.saveOrUpdate(recordingEntity);
                    ...
                }
            }, true);
        }
    return null;
}

这将导致:java.lang.ClassCastException: Recording cannot be cast to java.sql.Blob

然后我在没有@Lob的情况下尝试了它,我得到了: org.hibernate.MappingException: Could not determine type for: Recording, at table: RecordingEntity, for columns: [org.hibernate.mapping.Column(recording)]

编辑:

我试图保存对象,将它们转换为byte []并将byte []存储到数据库中。不幸的是,除非录制类实现Serializable接口,否则它是不可能的。我宁愿在不实现Serializable的情况下解决这个问题。

0 个答案:

没有答案