开发者

Hibernate error: Could not determine type for: com.mysql.jdbc.Blob

开发者 https://www.devze.com 2023-02-10 13:54 出处:网络
I\'m working on a project with Hibernate and MySQL. In one of my model objects, I declared a property \"image\" whose type is Blob, and I used com.mysql.jdbc.Blob. But when I ran that program, an erro

I'm working on a project with Hibernate and MySQL. In one of my model objects, I declared a property "image" whose type is Blob, and I used com.mysql.jdbc.Blob. But when I ran that program, an error occurred: org.hibernate.MappingException: Could not determine type for: com.mysql.jdbc.Blob, at table: SPOT, for columns: [org.hibernate.mapping.Column(image)]. Here is source code of data model:

    @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;
}
}

And here is the corresponding DDL of table SPOT:

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 */;

I searched on Internet and found a suggestion of using java.sql.Blob. But when I changed to that type, another error occurred, because in my program, I did some processes with XML on that model object, so it cannot handle the interface java.sql.Blob. So what I have to do to keep the data type com.mysql.jdbc.Blob and the program still run normally with Hibernate? Thank you so much.


I'd say that it's not right to depend on the implementation details for the JDBC driver. I would review your dependency, and try to make it a soft dependency. If you really need to keep this hard dependency, you'll need to implement an UserType capable of handling com.mysql.jdbc.Blob. I don't know the details about this implementation, but you can extend Hibernate's BlobType as MySQLBlobType, and annotate your model property with @Type annotation, specifying this 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

0

精彩评论

暂无评论...
验证码 换一张
取 消