我正在开发一个使用Hibernate和MySQL的项目。在我的一个模型对象中,我声明了一个类型为Blob的属性“image”,我使用了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?非常感谢你。
答案 0 :(得分:0)
我要说依赖JDBC驱动程序的实现细节是不对的。我会检查你的依赖,并尝试使它成为一个软依赖。如果你真的需要保持这种强硬依赖关系,那么你需要实现一个能够处理UserType
的{{1}}。我不知道有关此实现的详细信息,但您可以将Hibernate的BlobType扩展为MySQLBlobType,并使用com.mysql.jdbc.Blob
注释注释您的模型属性,并指定此@Type
: