无法执行OneToOne Mapping Hibernate

时间:2015-12-12 19:02:42

标签: java mysql spring hibernate hibernate-mapping

我试图使用Hibernate实现OneToOne映射。我使用Mysql作为DB。我有Product作为父级,ProductRestaurant作为其子级。试图在他们之间进行一对一的maping但是 在持久化的同时,获得以下异常:

2015-12-13 00:06:24 [http-nio-8080-exec-1] DEBUG org.hibernate.jdbc.A`enter code here`bstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2015-12-13 00:06:24 [http-nio-8080-exec-1] DEBUG o.h.util.JDBCExceptionReporter - **Could not execute JDBC batch update [insert into myproduct (business_category, in_stock, merchant_id, productRestaurant, product_id) values (?, ?, ?, ?, ?)]
java.sql.BatchUpdateException: Data truncation: Data too long for column 'productRestaurant' at row 1**
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1809) ~[mysql-connector-java-5.1.36.jar:5.1.36]
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1271) ~[mysql-connector-java-5.1.36.jar:5.1.36]
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) [hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) 

当我打印查询时,我发现了这一点。

 insert 
    into
        product
        (business_category, in_stock, merchant_id, productRestaurant, product_id) 
    values
        (?, ?, ?, ?, ?)

产品实体:

@Entity
@Table(name = "product")
public class Product implements IEntity {

    @Id
    @Column(name = "product_id")
    private String productId;

    @Column(name = "merchant_id")
    private String merchantId;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }


    public String getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(String merchantId) {
        this.merchantId = merchantId;
    }

    @Column(name = "business_category")
    private String businessCategory;

    @Column(name = "in_stock")
    private Boolean inStock;


    private ProductRestaurant productRestaurant;

    @OneToOne(fetch=FetchType.LAZY, mappedBy="product", cascade=CascadeType.ALL)
    public ProductRestaurant getProductRestaurant() {
        return productRestaurant;
    }

    public void setProductRestaurant(ProductRestaurant productRestaurant) {
        this.productRestaurant = productRestaurant;
    }
    public String getBusinessCategory() {
        return businessCategory;
    }

    public void setBusinessCategory(String businessCategory) {
        this.businessCategory = businessCategory;
    }

    public Boolean getInStock() {
        return inStock;
    }

    public void setInStock(Boolean inStock) {
        this.inStock = inStock;
    }

ProductRestaurant Entity

@Entity
@Table(name = "product_restaurant")
public class ProductRestaurant implements IEntity {

    @Id
    @Column(name="product_id")
    private String productId;

    /**
     * @return the productId
     */

    public String getProductId() {
        return productId;
    }
    public void setProductId(String productId) {
        this.productId = productId;
    }

    @Column(name = "category")
    private String category;

    @Column(name = "cuisine")
    private String cuisine;

    @Column(name = "title")
    private String title;

    @Column(name = "description")
    private String description;

    @Column(name = "price")
    private Double price;


    private Product product;

    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getCuisine() {
        return cuisine;
    }

    public void setCuisine(String cuisine) {
        this.cuisine = cuisine;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

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

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

创建的表:

CREATE TABLE IF NOT EXISTS `product` (
  `product_id` varchar(30) NOT NULL,
  `merchant_id` varchar(30) NOT NULL,
  `business_category` varchar(50) NOT NULL,
  `in_stock` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS `product_restraunt` (
  `product_id` varchar(30) NOT NULL,
  `category` varchar(50) NOT NULL,
  `cuisine` varchar(50) NOT NULL,
  `title` varchar(50) NOT NULL,
  `description` varchar(500) NOT NULL,
  `price` int(7) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



ALTER TABLE `product_restraunt`
  ADD CONSTRAINT `PRODUCT_RESTRAUNT_FK` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`);

我缺少什么?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您应该在annotationsProperty(getters or setters)上设置Fields,不要混用它。 在这种情况下,注释实体的字段将使提供者使用field access来获取和设置实体的状态,如果存在property acess,则提供者将忽略该实体

将双向@OneToOne的注释放在字段上,然后将其从getter中删除。

 @OneToOne(fetch = FetchType.LAZY)
 @PrimaryKeyJoinColumn
 private Product product;


public Product getProduct() {
    return product;
}

与另一个实体相同。