Reputation: 5199
I have written an application that parses an xml document using jaxb and then inserts into a database using JPA.
I have three JPA entities.
1.ItemEntity 2.PromotionEntity 3.SellPriceEntity
The item entity has a one to one relationship with the PromotionEntity and a one to one relationship with the SellPrice Entitiy.
When try to insert into the db using only the ItemEntity my application works and the item record is inserted into the db. However when I try to insert into the db using the ItemEntity, PromotionEntity and the SellPriceEntity I start to get errors.
org.apache.openjpa.persistence.PersistenceException: Incorrect integer value: '\xAC\xED\x00\x05sr\x00,org.apache.camel.example.etl.PromotionEntity$\x0C\xF5\xF1\x08\x0B\xA2\x81\x02\x00\x05L\x00\x02idt\x00\x10' for column 'PROMOTION_ID' at row 1 {prepstmnt 1554452939 INSERT INTO item (id, ATTRIBUTE_1, ATTRIBUTE_3, ATTRIBUTE_2, BRAND_LOGO_FILE_NAME, BRAND_NAME, CLASS_NO, DEFAULT_MARGIN, DESCRIPTION, EXTENDED_DESCRIPTION, EXTENDED_DESCRIPTION_2, GST_CODE, IMAGE_FILE_NAME, ITEM_NO, OUT_OF_STOCK_IND, PACK_QTY, PROMOTION_ID, SELL_PRICE_ID, SELLING_UNIT, SIZE_APPLICABLE, STOCK_AVAILABLE, SPPLR_NO, VOLUME, WEB_AGE_GROUP, WEB_COLOR_DESCRIPTION, WEB_DESCRIPTION, WEB_SIZE_DESCRIPTION, WEIGHT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)} [code=1366, state=HY000] FailedObject: org.apache.camel.example.etl.ItemEntity@333f8b4c
Here is how I have set my entity beans up...
@Entity(name = "item")
public class ItemEntity implements java.io.Serializable {
private static final long serialVersionUID = -9063279672222036437L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "ITEM_NO")
private String itemNo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="PROMOTION_ID")
private PromotionEntity promotion;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="SELL_PRICE_ID")
private SellPriceEntity sellPrice;
gets and sets...
Promotion Entity
@Entity(name = "promotion")
public class PromotionEntity implements java.io.Serializable {
private static final long serialVersionUID = 2597721500656837249L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "PROMOTION_ID")
private String promotionId;
@Column(name = "PROMOTION_PRICE")
private String promotionPrice;
gets and sets...
Sell Price Entity
@Entity(name = "sell_price")
public class SellPriceEntity implements java.io.Serializable {
private static final long serialVersionUID = -205334787672950850L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "SELL_PRICE_EFFECTIVE_DATE_1")
private String sellPriceEffectiveDateOne;
@Column(name = "SELL_PRICE_1")
private String sellPriceOne;
gets and sets...
I believe that I have defined the relationship fields properly so not sure what is going wrong. On trying to debug through the JDBCStoreManager class I can see that the sql being executed is....
com.mysql.jdbc.JDBC4PreparedStatement@6d66cc49:
INSERT INTO item (
id,
ATTRIBUTE_1,
ATTRIBUTE_3,
ATTRIBUTE_2,
BRAND_LOGO_FILE_NAME,
BRAND_NAME,
CLASS_NO,
DEFAULT_MARGIN,
DESCRIPTION,
EXTENDED_DESCRIPTION,
EXTENDED_DESCRIPTION_2,
GST_CODE, I
MAGE_FILE_NAME,
ITEM_NO,
OUT_OF_STOCK_IND,
PACK_QTY,
PROMOTION_ID,
SELL_PRICE_ID,
SELLING_UNIT,
SIZE_APPLICABLE,
STOCK_AVAILABLE,
SPPLR_NO,
VOLUME,
WEB_AGE_GROUP,
WEB_COLOR_DESCRIPTION,
WEB_DESCRIPTION,
WEB_SIZE_DESCRIPTION,
WEIGHT)
VALUES (701, '', '', '', '', '', '350', '.00', 'KHOMBU APFOOTA KOKO HIGH', '', '', '1', '', '93501250080', 'Y', '0', ** STREAM DATA **, ** STREAM DATA **, 'Each', 'Y', '0', 'KHOMBU', '.0000', '', 'Black', '', '8', '.00')
Where I should be inserting promotion_id and sell_price_id it is telling me ** STREAM DATA **. Is that right? Maybe this is why I'm getting a data type error.
thanks
Upvotes: 0
Views: 1932
Reputation: 21165
It is not clear what you are after from the description, which also conflicts with the model you posted. A joincolumn is used to specify the foreign key field name as well as the primary key it references in the target entity. But JPA requires this to be the primary key in the referenced entity, so your mappings end up using "ITEM"."PROMOTION_ID" to reference "PROMOTION"."ID". You get the exception because on of the "PROMOTION_ID" fields doesn't exist on one of the tables - either the item or promotion tables.
Decide which table will have the foreign key to the parents table and go from there. I'm not sure it makes sense to have a single auto generated pk value reused for the 3 entities involved in the model shown, as it means the other two entitities can never exist with out the one who gets the id set. For example, there would be no way to create a new promotion and keep the old one around. That said, it would be possible in JPA 2.0 to set a relationship as the @ID. So you could remove the id from item like this:
@Column(name = "ITEM_NO")
private String itemNo;
@Id
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="PROMOTION_ID")
private PromotionEntity promotion;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="SELL_PRICE_ID")
private SellPriceEntity sellPrice;
This would have item use the generated long id value from promotion as its id as well, and store it in the "PROMOTION_Id" field. You could do something similar to SellPriceEntity so it has a relationship to item or promotion that it could use to get its id set.
Upvotes: 1