无法执行 OneToOne 映射 Hibernate

Unable to perform OneToOne Mapping Hibernate

我正在尝试使用 Hibernate 实现 OneToOne 映射。我正在使用 Mysql 作为数据库。我将 Product 作为父级,将 ProductRestaurant 作为其子级。试图在它们之间建立一对一的映射但是 坚持时,出现以下异常:

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

产品餐厅实体

@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`);

我错过了什么?提前致谢。

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

将双向@OneToOne的注释放在字段上,并从getters中删除。

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


public Product getProduct() {
    return product;
}

与另一个实体相同。