gpt4 book ai didi

java - 无法执行 OneToOne 映射 Hibernate

转载 作者:行者123 更新时间:2023-11-29 02:52:29 26 4
gpt4 key购买 nike

我正在尝试使用 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`);

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

最佳答案

您应该在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;
}

与其他实体相同。

关于java - 无法执行 OneToOne 映射 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34243732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com