Hibernate在查询中使用随机未指定的列名

时间:2012-07-19 17:36:24

标签: java mysql hibernate

我在数据库架构中的两个表的hibernate映射时遇到问题。一个叫BINARY_DATA_CONTENTS,另一个叫BINARY_DATABINARY_DATA只有一个名称字段和一个指向BINARY_DATA_CONTENTS的指针。这就是模式的样子:

BINARY_DATA_CONTENTS:

CREATE TABLE `BINARY_DATA_CONTENTS` (
  `BINARY_DATA_CONTENTS_ID` varchar(255) NOT NULL,
  `BINARY_DATA` longblob, -- stores the data
  PRIMARY KEY (`BINARY_DATA_CONTENTS_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

BINARY_DATA:

CREATE TABLE `BINARY_DATA` (
  `BINARY_DATA_ID` varchar(255) NOT NULL DEFAULT '',
  `BINARY_DATA_NAME` varchar(255) DEFAULT NULL, -- just a filename
  `BINARY_DATA_CONTENTS_ID` varchar(255) DEFAULT NULL, -- points to BINARY_DATA_CONTENTS
  PRIMARY KEY (`BINARY_DATA_ID`),
  KEY `BINARY_DATA_CONTENTS_FK` (`BINARY_DATA_CONTENTS_ID`),
  CONSTRAINT `BINARY_DATA_CONTENTS_FK` FOREIGN KEY (`BINARY_DATA_CONTENTS_ID`) REFERENCES `BINARY_DATA_CONTENTS` (`BINARY_DATA_CONTENTS_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我为每个表都有Java类。

BinaryDataContents:

@Entity
@Table(name="BINARY_DATA_CONTENTS")
@Proxy(lazy=true)
@Inheritance(strategy= InheritanceType.JOINED)
public class BinaryDataContents implements Serializable, Persistable<BinaryDataContents>, Cloneable {
    private static final long serialVersionUID = /** blah */;

    @Id
    @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid", strategy="uuid")
    @Column(name="BINARY_DATA_CONTENTS_ID")
    private String id;

    @Column(name="BINARY_DATA", length=32*1024*1024, columnDefinition="longblob") @Lob
    private byte[] contents;

    /**
     * Hibernate constructor
     */
    public BinaryDataContents() { }

    public BinaryDataContents(byte[] contents) {
        this.contents = ArrayUtils.clone(contents);
    }

    public byte[] getContents() {
        return contents;
    }
    // etc...

BinaryData:

@Entity
@Table(name="BINARY_DATA")
@Inheritance(strategy=InheritanceType.JOINED)
public class BinaryData implements Serializable, Persistable<BinaryData>, Cloneable {

    private static final long serialVersionUID = 2154854247822039938L;

    @Id @Column @GeneratedValue(generator="uuid") @GenericGenerator(name="uuid", strategy="uuid")
    private String id;

    @Column
    private String binaryDataName;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="BINARY_DATA_CONTENTS_ID", nullable=true) @ForeignKey(name="BINARY_DATA_CONTENTS_FK")
    private Collection<BinaryDataContents> binaryDataContents;

    @Transient
    private String cacheId;

    /**
     * Hibernate constructor
     */
    public BinaryData() { this.binaryDataContents = Sets.newHashSet(new BinaryDataContents()); }

    /**
     * Create a new instance of BinaryData.
     *
     * @param binaryDataData <code>byte []</code> of the file to store.
     * @param binaryDataName Name of attachment
     */
    public BinaryData(byte[] binaryDataData, String binaryDataName) {
        this.binaryDataContents = Sets.newHashSet(new BinaryDataContents(ArrayUtils.clone(binaryDataData)));
        this.binaryDataName = binaryDataName;
    }

    /**
     * Returns the BinaryData byte stream.
     *
     * @return binaryDataContents byte stream
     */
    public byte[] getData() {
        return binaryDataContents.iterator().next().getContents();
    }

    /**
     * Returns the BinaryData name.
     *
     * @return BinaryData name
     */
    public String getBinaryDataName() {
        return binaryDataName;
    }
    // etc...

我希望BinaryDataContents延迟加载,只有在需要时才会加载(getData中的BinaryData)。我不确定我是否正确设置了所有注释,因为我在尝试保存新的BinaryData对象时遇到错误:

PersistenceException: Failed to commit Caused by: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update Caused by: java.sql.BatchUpdateException: Unknown column 'AUTHORISATION_REQUEST_ID' in 'field list'

这很奇怪,因为在这些表或类中没有名为AUTHORISATION_REQUEST_ID的列或字段。似乎hibernate已经变得混乱,并且在SQL中抛出了奇怪的列名:

insert into BINARY_DATA (BINARY_DATA_NAME, AUTHORISATION_REQUEST_ID) values (?, ?)

有关可能导致此问题的任何想法?谢谢。

更新1

如果我使用@OneToOne注释,则错误会略有变化。 BINARY_DATA_CONTENTS插入似乎生成正确的SQL:

insert into BINARY_DATA_CONTENTS (BINARY_DATA, BINARY_DATA_CONTENTS_ID) values (?, ?)

但是,BINARY_DATA插入内容仍然包含随机列名称:

insert into BINARY_DATA (BINARY_DATA_CONTENTS_ID, BINARY_DATA_NAME, AUTHORISATION_REQUEST_ID) values (?, ?, ?)

更新2

看来来自无处的strage AUTHORISATION_REQUEST_ID列会根据我使用我的应用程序注册所有hibernate带注释的类的顺序而更改(有时它是DATABASE_UPGRADE_ID)。应用程序保留了这些类的列表,并将它们传递给某种疯狂的PersistenceConfiguration,它会用它来做各种奇怪的事情。这可能是由......引起的......

1 个答案:

答案 0 :(得分:0)

不知道这个AUTHORIZATION_REQUEST_ID来自哪里。可能是因为你没有告诉我们。但是你的映射显然是错误的。二进制数据具有二进制数据内容的外键。

所以这意味着二进制数据可能只有一个二进制数据内容。因此,如果多个二进制数据可能共享相同的内容,则应该有一个ManyToOne。否则,你应该有一个OneToOne。 OneToMany当然不是你想要的,而且你使用Set并且只对它的第一个元素感兴趣的事实强烈表明它是错误的。这是它应该如何:

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "BINARY_DATA_CONTENTS_ID", nullable=true) 
@ForeignKey(name = "BINARY_DATA_CONTENTS_FK")
private BinaryDataContents binaryDataContents;