java - Hibernate OneToMany MySQLIntegrityConstraintViolationException: Duplicate entry -
i have 2 java hibernate entities:
@entity public class match_soccer extends match{ @id @generatedvalue(strategy=generationtype.auto) private int mid; ... and other one:
@entity public class algo { @id @generatedvalue(strategy=generationtype.auto) private int id; @onetomany private list<match_soccer> matches = new arraylist<match_soccer>(); ... and if try save 2 different algo entities matches list duplicate entry '6028' key 'uk_auyvi1qkpdtaqrpuyv9je5rda' exception.
hibernate create in database 3 tables: algo match_soccer , table:
algo_match_soccer columns: algo1_id int(11) matches_mid int(11) pk my goal assign list of matches algo. matches can in 2 different algo objects.
algo a1 = new algo(); algo a2 = new algo(); soccerdao sd = new soccerdao(); list<match_soccer> ms = sd.getmatches(datefrom,dateto); // matches database a1.setmatches(ms); a2.setmatches(ms); i use function insert:
public void insertalgo(algo1 a){ try { session.begintransaction(); session.save(a); session.gettransaction().commit(); } catch (runtimeexception e) { session.gettransaction().rollback(); session.clear(); throw e; }
matches can in 2 different algo objects
then don't have onetomany association, manytomany association. since algo has several matches, , match belongs several algos.
change mapping, , change database schema accordingly: pk of join table should pair of ids, , not match id.
Comments
Post a Comment