java


Hibernate cascade save


I have a problem with Hibernate.
See my data model
I have class - "Ticket"...
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "ticketComposition",
joinColumns = { #JoinColumn(name = "ticketId")},
inverseJoinColumns = { #JoinColumn(name = "tripsSiteId")})
private Set<TripsSite> tripsSites = new HashSet<TripsSite>();
and when I'm trying to save new ticket...
Ticket ticket = new Ticket();
User user = userService.findBySSO(userName);
ticket.setUser(user);
ticket.setTripsSites(new HashSet<>(tripsSites));
persist(ticket);
I get the following error: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (sbb.ticketcomposition, CONSTRAINT ticket FOREIGN KEY (ticketId) REFERENCES ticket (id))
How can i resolve this problem?
Assuming you have something like this defined on the TicketSite entity:
#ManyToMany(mappedBy="tripSites")
private Set<Trip> trips = new HashSet<Trip>();
While persisting, you have to set the dependencies on both sides of the #ManyToMany mapping:
Ticket ticket = new Ticket();
User user = userService.findBySSO(userName);
ticket.setUser(user);
ticket.setTripsSites(new HashSet<>(tripsSites));
for(TicketSite ticketSite: ticketSites){
ticketSite.getTickets().add(ticket);
}
persist(ticket);
I am assuming that the ticketSites set has been previously retrieved from the database and has been persisted before this transactional method started.

Related Links

Android getSupportActionBar().setHomeAsUpIndicator() how do i resize the image? its way to big
Does XStream support inheritance?
Android Volley file sending with Apache: NoClassDefFoundError: Failed resolution of: ContentType
Keeping A Score
Handle multipart/mixed response with Spring RestTemplate
Getting error with Appium automation
Error with PreparedStatement
Testing if custom exception was thrown when a run time exception is thrown using mockito
javax.xml.ws.WebServiceException: Could not find service named
Android get the Intent when implementing RecognitionListener
How to stack components inside a vertical cell in the MigLayout?
Use “R” instead of “ZAR” for South African Currency
Why doesn't PHP allow class variables to be assigned as objects?
How to SSH tunnel and connect to a database using IntelliJ and JDBC?
AWS S3 returns “POST 400 bad request” for fileupload using plupload
How can C# override LDAP server limit, but not Java

Categories

HOME
deployment
gremlin
plone
hashmap
iot
bpmn
rdf
webpack-2
installshield
append
windows-10-universal
metatrader4
seaborn
serilog
undefined
django-cms
cloudhub
dcevm
excel-2007
php-openssl
android-widget
tdd
qhull
centos6.5
elasticsearch-ruby
facebook-apps
google-rich-snippets
phonegap
lxd
picasso
revolution-slider
dartium
qwerty
total-commander
rainbowtable
xml-documentation
return-value
statsd
nativeapplication
worker-thread
nodebb
libusb-win32
probability-density
trash
fputcsv
simplewebrtc
wapiti
android-listview
microbenchmark
sniffer
unity3d-gui
remobjects
modalpopup
oxwall
asp.net-dynamic-data
device-manager
document-classification
hippomocks
undefined-reference
hyprlinkr
resty-gwt
android-screen-support
specification-pattern
typoscript2
runas
regsvr32
netbeans-6.9
punbb
web-application-design
getresponsestream
filtered-index
meego
gacutil
data-acquisition
windows-live-messenger

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile