Reputation: 21
I was trying to implement OneToOne mapping using Hibernate. I am using Mysql as DB. I have Product as parent and ProductRestaurant as its child. Trying to get one to one maping between them but While persisting, getting the following exception:
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)
When i print the queries, i found this.
insert
into
product
(business_category, in_stock, merchant_id, productRestaurant, product_id)
values
(?, ?, ?, ?, ?)
Product Entity:
@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;
}
Tables Created:
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`);
What am i missing? Thanks in advance.
Upvotes: 2
Views: 146
Reputation: 4158
You should either set the annotations
on the Property(getters or setters)
or Fields
, don't mix it.
In this case annotating the fields of the entity will cause the provider to use field access
to get and set the state of the entity, if a property acess
is present it will be ignored by the provider
Put the annotations of the bidirectional @OneToOne
on the fields and remove it from the getters.
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private Product product;
public Product getProduct() {
return product;
}
the same thing with the other entity.
Upvotes: 1