JPA/Hibernate: How insert a default value for a @ManyToOne where the value represents a null entry -


i'm new jpa/hibernate. issue involves couple of existing database tables shown below:

create table `person` (   `personid` int(11) not null,   `name` varchar(255) character set latin1 not null default '',   `addressid` int(11) not null default 0,   primary key (`personid`) );  create table `address` (   `addressid` bigint(20) not null,   `address` varchar(255) character set latin1 not null default '',   primary key (`addressid`) ); 

the entities implemented such:

@entity public class person {   @id   private integer personid;    private string name;    @manytoone   @joincolumn(name="addressid")   private address address; }  @entity public class address {   @id   private integer addressid;    private string address; 

}

multiple people can live in same address. person can not have address. addressid person table optional field. there no addressid = 0 in address database. value 0 used if null parameter.

my issue getting happen when try persist person table addressid = 0.

if set address object within person null, following error:

"addressid cannot null"

if set address object have addressid of 0, other transientobjecterror, returned because there's no entry in address table addressid = 0.

i tried using @prepersist try attempt set default values before object gets persisted, however, same 2 errors above. same thing when add optional=true in @manytoone.

i don't think inserting dummy address id=0 idea.

any ideas if it's possible use @manytoone annotation , still have default value of 0 0 not actual entry in database?

if have keep these 0 instead of null maybe can try customtype. see here: hibernate many-to-one foreign key default 0


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -