【问题标题】:Hibernate error: Could not determine type for: com.mysql.jdbc.Blob休眠错误:无法确定类型:com.mysql.jdbc.Blob
【发布时间】:2011-06-28 01:56:45
【问题描述】:

我正在开发一个使用 Hibernate 和 MySQL 的项目。在我的一个模型对象中,我声明了一个类型为 Blob 的属性“图像”,并且我使用了 com.mysql.jdbc.Blob。但是当我运行该程序时,发生错误:org.hibernate.MappingException:无法确定类型:com.mysql.jdbc.Blob,表:SPOT,列:[org.hibernate.mapping.Column(image) ]。 这是数据模型的源代码:

    @Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "SPOT", catalog = "ar", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
@XmlRootElement(name = "spot")
public class Spot extends BaseIdObject {
    private Double axisX;
    private Double axisY;
    private String address;
    private String spotType;
    private String description;
    private String phoneNumber;
    private String website;
    private Blob image;

    @Column(name = "axis_x", precision = 22, scale = 0)
    @NotNull
    public Double getAxisX() {
        return this.axisX;
    }

    public void setAxisX(Double axisX) {
        this.axisX = axisX;
    }

    @Column(name = "axis_y", precision = 22, scale = 0)
    @NotNull
    public Double getAxisY() {
        return this.axisY;
    }

    public void setAxisY(Double axisY) {
        this.axisY = axisY;
    }

    @Column(name = "address", length = 200)
    @NotNull
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "spot_type", length = 50)
    @NotNull
    public String getSpotType() {
        return this.spotType;
    }

    public void setSpotType(String spotType) {
        this.spotType = spotType;
    }

    @Column(name = "description", length = 2000)
    public String getDescription() {
        return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

@Column(name = "phone_number", length = 30)
public String getPhoneNumber() {
    return this.phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}
}

这里是表SPOT对应的DDL:

DROP TABLE IF EXISTS `spot`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `spot` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(200) DEFAULT NULL,
  `AXIS_X` double NOT NULL,
  `AXIS_Y` double NOT NULL,
  `ADDRESS` varchar(200) DEFAULT NULL,
  `SPOT_TYPE` varchar(50) NOT NULL,
  `DESCRIPTION` varchar(2000) DEFAULT NULL,
  `PHONE_NUMBER` varchar(30) DEFAULT NULL,
  `WEBSITE` varchar(200) DEFAULT NULL,
  `IMAGE` blob,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `SPOT_ID_UNIQUE` (`ID`),
  UNIQUE KEY `SPOT_NAME_UNIQUE` (`NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

我在互联网上搜索,发现使用 java.sql.Blob 的建议。但是当我改成那个类型时,又出现了一个错误,因为在我的程序中,我在那个模型对象上用XML做了一些处理,所以它不能处理接口java.sql.Blob。那么我必须做些什么来保持数据类型 com.mysql.jdbc.Blob 并且程序仍然可以在 Hibernate 中正常运行?非常感谢。

【问题讨论】:

    标签: mysql hibernate hibernate-mapping


    【解决方案1】:

    我会说依赖 JDBC 驱动程序的实现细节是不对的。我会审查您的依赖关系,并尝试使其成为软依赖关系。如果你真的需要保持这种硬依赖,你需要实现一个能够处理com.mysql.jdbc.BlobUserType。我不知道这个实现的细节,但是你可以将 Hibernate 的 BlobType 扩展为 MySQLBlobType,并用@Type 注释来注释你的模型属性,指定这个MySQLBlobType

    https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/type/BlobType.java

    https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/test/java/org/hibernate/test/annotations/type/Dvd.java

    【讨论】:

      猜你喜欢
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多