java - Switching foreign key in hibernate is not working -
tldr;
i'm using spring boot , jpa.
i want switch foreign key of object, in case switching category of vehicle.
but when try hibernate interprets if i'm trying change primary key of category object instead of switching foreign key , error
org.hibernate.hibernateexception:identifier of instance of abc.package.mode.category altered 1 2
i have entity category i'm using categorizing vehicle entity object.
@entity public class category { @id private long id; private string name; }
here vehicle class needs categorized.
@entity public class vehicle { @id @generatedvalue(strategy = generationtype.auto, generator="dish_seq") private long id; private string name; private integer price; @manytoone(fetch = fetchtype.eager, cascade=cascadetype.detach) private category category; }
lets there's 3 categories,
- 'sedan'
- 'convertible'
- 'hatchback'
if have car object, nissan-pt76, $30000, category: [id:1, name:sedan]
when try change category manually [id:2, name:convertible] , persist it, get
org.hibernate.hibernateexception:identifier of instance of abc.package.mode.category altered 1 2
i cannot switch 1 existing object another. have tried in internet couldn't find right keywords search kind of relationship in hibernate, or not allow kind of relationship @ all?
add column reference category field in vehicle class
@joincolumn(name = "category_id", nullable = false)
Comments
Post a Comment