Mapping varchar type of a SQL column to a java.net.Url of java with Hibernate 4 mapping -
i have column (named "url") in table values urls. stored "http://www.test.com" while other "blogspot.sometest.com" (the table not mine, way, can't change structure).
on project, stored content of "url" column java.net.url type property of structured object (e.g. page(string name,url url);). so, if possible, don't wanna modify project.
i want, using hibernate, able single record specific id. exemple code:
@override public t selectrow(serializable id){ t object = null; try { opensession(); trns = session.begintransaction(); criteria = session.createcriteria(cl); criteria.add(org.hibernate.criterion.restrictions.eq("doc_id",id)); //this line of code launch exception: org.hibernate.hibernateexception: unable convert string [4bid.it] url : java.net.malformedurlexception: no protocol: 4bid.it //because of urls there no protocol... list<t> results = criteria.list(); //same object = (t) criteria.setfirstresult((integer) id); //the next line of code work return null url without protocol object = (t) session.load(cl, id); } catch (runtimeexception e) { if (trns != null) { trns.rollback();} systemlog.exception(e); } { session.flush(); session.close(); } return object; } so, can create specific java class implements org.hibenrate.usertype , try resolve that, read in hibernate documentation exists urltype (https://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/type/urltype.html) , urltype mapping between varchar , url, must stupid, don't how can use mapping varchar of table java.net.url object of programm. if anyway know trick intercept value returned hibernate , append string "http://" welcome either.
ty in advance. greetings.
update 1: brute force solution problem execute 2 sql query: before crud hibernate operation:
update mytable set url= concat('http://',url) not url 'http://%'; run hibernate operation.
after crud hibernate operation:
update mytable set url = replace(url, 'http://', '') url '%http://%'; update2 i'have tried @preupdate , @prepersist annotation on seturl(url url) this:
@preupdate @prepersist public void seturl(url url) { if(url.tostring().contains("://")) { this.url = url; }else{ try { this.url = new url("http://"+url.tostring()); } catch (malformedurlexception e) { e.printstacktrace(); } } now work fine when make update,remove,persist operation , if want "@preload" (note: annotation not exists)for convert sql value "www.test.com" "http://www.test.com" before storage on java object simple thing found use hibernate interceptor.
the urltype supported , maps varchar column java.net.url property.
all need use simple mapping such:
@column(name = "url") private url url;
Comments
Post a Comment