java - @Column insertable, updateble don't go well with Spring JPA? -
scenario : have 3 tables, offer, channel , offer_channels. channel lookup table, i.e, values in table can neither inserted nor updated application. offer can contain 1 or many channels. use channel table values populate dynamic checkboxes. anyways, here have :
@entity @table(name = "offer") @cache(usage = cacheconcurrencystrategy.nonstrict_read_write) public class offer implements serializable { // offer id @id @generatedvalue(strategy = generationtype.auto, generator = "offer_seq_gen") @column(name = "offer_id") private long offerid; @manytomany(cascade = cascadetype.all) @jointable(name = "offer_channels", joincolumns = { @joincolumn(name = "offer_id") }, inversejoincolumns = { @joincolumn(name = "channel_id") }) private set<channel> channels = new hashset<channel>(); //other fields , corresponding getters , setters }
here channel entity :
@entity @table(name = "channel") public class channel implements serializable { private static final long serialversionuid = 1l; @notnull @id @column(name = "channel_id", insertable=false, updatable=false) private long channelid; @column(name = "channel_name", insertable=false, updatable=false) private string channelname; //getters , setters }
now, when user creates offer, need insert row in offer table , offer_channels tables , nothing(no updates/inserts) channel table. initially, 3 happen, achive "do nothing channel table" part, put insertable=false , updateable=false on channel table columns , worked charm. used plain hibernates this. mean wrote standalone java application , main class add offer useing hibernate's session.save(offer). ran following queries :
hibernate: insert offer hibernate: insert offer_channels
alright, now, have rest service using spring's jpa repository save information , have same domain objects setup. now, when add offer, runs :
hibernate: insert offer hibernate: insert channel ( failing here obviously. don't want step happen)
my question : 1. why is trying write channel table though gave insertable=false in domain object, , happening spring jpa setup. hibernate setup works fine. 3. doesn't @jointable/ @onetomany / insertable / updateble , go spring jpa repository ? missing here ?
update :
@service @transactional public class offerservice { @inject private offerrepository offerrepository; public offer saveofferinformation(offer offer) { log.debug("saving offer info.."); log.debug("offer object :"+offer); return offerrepository.save(offer); } }
repo :
public interface offerrepository extends jparepository<offer, long> { list<offer> findbybuysku(string buysku); }
and in rest service im injecting service , calling it, no business logic in rest service. right im getting , reason is trying insert record channel table:
exception: "org.springframework.dao.dataintegrityviolationexception" message: "could not execute statement; sql [n/a]; constraint [pvs_owner.channel_pk]; nested exception org.hibernate.exception.constraintviolationexception: not execute statement"
Comments
Post a Comment