2011-02-16 40 views
0

我正在使用Hibernate和MySQL進行項目。在我的一個模型對象中,我聲明瞭一個類型爲Blob的屬性「image」,並使用了com.mysql.jdbc.Blob。但是,當我運行該程序時,發生了一個錯誤:org.hibernate.MappingException:無法確定類型爲:com.mysql.jdbc.Blob,在表:SPOT,爲列:[org.hibernate.mapping.Column(image) ]。 下面是數據模型的源代碼:休眠錯誤:無法確定類型爲:com.mysql.jdbc.Blob

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

這裏是表點的相應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

我想說依賴於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