Monday 15 August 2011

java - Implement Generic Abstract Entity class with dao interface and implemantation -



java - Implement Generic Abstract Entity class with dao interface and implemantation -

hey have generic model/entity class download id of type long element db. method this:

public class genericmodel { @id @generatedvalue(strategy = generationtype.auto) public long id; public static genericmodel getby(long id) { homecoming jpa.em().find(genericmodel.class, id); } }

but in generic model extended kid model class i'have dynamically declare entity class name find in db.

i have generic entity class have mutual methods getbyid(). , generic class extended concret entity class. not have write in each model class same method, cause inherited generic class –

how can achive this?

here dao interface. not quite sure of it:

public interface genericmodeldao<t> { public void add(t entityclass); public void update(t entityclass); public void delete(long id); public t get(long id); public list<t> get(); }

and dao implementation class of interface

@repository public class genericmodeldaoimpl <t extends genericmodel> implements genericmodeldao { public class<t> entityclass; genericmodeldaoimpl(){ setentityclass(((class) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0])); } public void setentityclass(class<t> entityclass) { this.entityclass = entityclass; } @autowired private sessionfactory sessionfactory; private session getcurrentsession() { homecoming sessionfactory.getcurrentsession(); } @override public t get(long id) { homecoming (t) getcurrentsession().get(entityclass, id); } @override public void delete(long id) { t entityclass = get(id); getcurrentsession().delete(entityclass); } @override public list<t> get() { homecoming getcurrentsession().createquery("from " + entityclass ).list(); } @override public void add(object entityclass) { getcurrentsession().save(entityclass); } @override public void update(object entityclass) { getcurrentsession().update(entityclass); } }

and genericmodel class

@mappedsuperclass public abstract class genericmodel<t extends genericmodel> { @id @generatedvalue(strategy = generationtype.auto) public long id; public long getid() { homecoming id; } public void setid(long id) { this.id = id; } }

please give me help 1 time more :d

you cannot utilize generics parameters in static method (see http://stackoverflow.com/a/936951/1643132 more details), have remove static keyword getby() method.

as first step, can introduce generics in genericmodel class:

public abstract class genericmodel<t extends genericmodel> { @id @generatedvalue(strategy = generationtype.auto) public long id; public t getby(long id) { homecoming jpa.em().find(????, id); } }

the problem is, t.class not work (in getby() method). java.lang.reflect.parameterizedtype, can retrieve t class @ runtime. update genericmodel :

@mappedsuperclass public abstract class genericmodel<t extends genericmodel> { private class<t> entityclass; genericmodel(){ entityclass = ((class) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0]); } @id @generatedvalue(strategy = generationtype.auto) public long id; public t getby(long id) { homecoming jpa.em().find(entityclass, id); } public long getid() { homecoming id; } }

your kid entity may like:

@table(name="child_model") @javax.persistence.entity public class childmodel extends genericmodel<childmodel> { @column private string data; public string getdata() { homecoming data; } public void setdata(string data) { this.data = data; } }

java spring jpa model entity

No comments:

Post a Comment