【问题标题】:Unable to perform OneToOne Mapping Hibernate无法执行 OneToOne 映射休眠
【发布时间】:2016-03-18 13:24:01
【问题描述】:

我试图使用 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;
    }

ProductRestaurant 实体

@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 行的列 'productRestaurant' 的数据太长”。因此,您尝试插入的内容太长而无法存储此列。
  • productRestaurant 是实体对象。它不是产品 tablr 中的列。我在产品实体中有它的 getter setter。 Hibernate 正在尝试直接传递对象,我认为这不应该发生

标签: java mysql spring hibernate hibernate-mapping


【解决方案1】:

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

将双向@OneToOne的注解放在字段上,并从getter中移除。

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


public Product getProduct() {
    return product;
}

与其他实体相同。

【讨论】:

  • 非常感谢...这有效。但是有什么区别。??同样在数据库中,添加了额外的列。与 Product 表中一样,productRestaurant 添加了 null 值,同样在 ProductRestaurant 表中,product 列添加了 null 值。它是这样工作的吗?我是新手,所以没有太多信息..
  • @Rohit 你可以在这里找到不同之处:howtodoinjava.com/2015/04/21/…
猜你喜欢
  • 2014-12-27
  • 2014-03-12
  • 2017-11-13
  • 2011-05-21
  • 1970-01-01
  • 2019-05-02
  • 2015-05-11
  • 1970-01-01
相关资源
最近更新 更多