java - DAO and Spring Autowired -
i tried create abstract dao. use spring + hibernate. here's code.
main class configuration:
package ru.makaek.growbox.api; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.autoconfigure.orm.jpa.hibernatejpaautoconfiguration; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.core.env.environment; import org.springframework.jdbc.datasource.drivermanagerdatasource; import org.springframework.orm.hibernate5.hibernatetransactionmanager; import org.springframework.orm.hibernate5.localsessionfactorybean; import org.springframework.transaction.annotation.enabletransactionmanagement; import javax.sql.datasource; @componentscan(value = "ru.makaek.growbox") @enableautoconfiguration(exclude = hibernatejpaautoconfiguration.class) @enabletransactionmanagement @springbootapplication public class application { @autowired private environment env; public static void main(string[] args) { springapplication.run(application.class, args); } @bean public datasource getdatasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname(env.getrequiredproperty("datasource.driver")); datasource.seturl(env.getrequiredproperty("datasource.url")); datasource.setusername(env.getrequiredproperty("datasource.username")); datasource.setpassword(env.getrequiredproperty("datasource.password")); return datasource; } @bean public localsessionfactorybean getsessionfactory() { localsessionfactorybean sessionfactory = new localsessionfactorybean(); sessionfactory.setdatasource(getdatasource()); sessionfactory.setpackagestoscan(new string[]{"ru.makaek.growbox"}); return sessionfactory; } @bean public hibernatetransactionmanager gettransactionmanager(sessionfactory sessionfactory) { hibernatetransactionmanager txmanager = new hibernatetransactionmanager(); txmanager.setsessionfactory(sessionfactory); return txmanager; } } rest controller
package ru.makaek.growbox.api.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.*; import ru.makaek.growbox.api.model.data.entities.device; import ru.makaek.growbox.api.service.istructureservice; @restcontroller public class devicecontroller extends abstractcontroller { @autowired istructureservice structureservice; @requestmapping(value = "/devices", method = requestmethod.post) public answer adddevice(@requestbody device device) { structureservice.adddevice(device); return ok("device has been added"); } @requestmapping(value = "/devices", method = requestmethod.get) public answer getdevices() { return ok(structureservice.getdevices()); } @requestmapping(value = "/devices/{deviceid}", method = requestmethod.get) public answer getdevice(@pathvariable long deviceid) { return ok(structureservice.getdevice(deviceid)); } } service layer. interface
package ru.makaek.growbox.api.service; import ru.makaek.growbox.api.model.data.entities.device; import java.util.list; public interface istructureservice { void adddevice(device device); list<device> getdevices(); device getdevice(long deviceid); } service layer. implementation
package ru.makaek.growbox.api.service; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import ru.makaek.growbox.api.model.data.dao.base.idao; import ru.makaek.growbox.api.model.data.entities.device; import java.util.list; @service @transactional public class structureservice implements istructureservice { idao<device> devicedao; @autowired public void setdao(idao<device> dao) { devicedao = dao; dao.setclazz(device.class); } @override public void adddevice(device device) { devicedao.create(device); } @override public list<device> getdevices() { return devicedao.findall(); } @override public device getdevice(long deviceid) { return devicedao.findone(deviceid); } } entity
package ru.makaek.growbox.api.model.data.entities; import lombok.data; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; @entity(name = "devices") @data public class device extends baseentity { @id @generatedvalue(strategy = generationtype.identity) private long id; private string name; } dao. interface
package ru.makaek.growbox.api.model.data.dao.base; import ru.makaek.growbox.api.model.data.entities.baseentity; import java.util.list; public interface idao<t extends baseentity> { t findone(final long id); void setclazz(class<t> clazz); list<t> findall(); void create(final t entity); t update(final t entity); void delete(final t entity); void deletebyid(final long entityid); } abstract dao
package ru.makaek.growbox.api.model.data.dao.base; import org.hibernate.session; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import ru.makaek.growbox.api.model.data.entities.baseentity; import ru.makaek.growbox.api.util.gbexception; import java.util.list; public abstract class abstractdao<t extends baseentity> implements idao<t> { private class<t> clazz; @autowired private sessionfactory sessionfactory; public final void setclazz(class<t> clazz) { this.clazz = clazz; } public t findone(long id) { try { return (t) getcurrentsession().get(clazz, id); } catch (exception e) { throw new gbexception.internalerror(e.getmessage()); } } public list<t> findall() { try { return getcurrentsession().createquery("from " + clazz.getname()).list(); } catch (exception e) { throw new gbexception.internalerror(e.getmessage()); } } public void create(t entity) { try { getcurrentsession().persist(entity); } catch (exception e) { throw new gbexception.internalerror(e.getmessage()); } } public t update(t entity) { try { return (t) getcurrentsession().merge(entity); } catch (exception e) { throw new gbexception.internalerror(e.getmessage()); } } public void delete(t entity) { try { getcurrentsession().delete(entity); } catch (exception e) { throw new gbexception.internalerror(e.getmessage()); } } public void deletebyid(long entityid) { try { t entity = findone(entityid); delete(entity); } catch (exception e) { throw new gbexception.internalerror(e.getmessage()); } } protected final session getcurrentsession() { return sessionfactory.getcurrentsession(); } } dao. implementation
package ru.makaek.growbox.api.model.data.dao; import org.springframework.stereotype.repository; import ru.makaek.growbox.api.model.data.dao.base.abstractdao; import ru.makaek.growbox.api.model.data.entities.device; @repository public class devicedao extends abstractdao<device> { } i have 1 trouble. when call get http://host:port/devices api method have null in clazz variable in abstractdao.findall() method. when debugging code found 1 interesting thing: in service layer method devicedao.getclazz() returned needed clazz (not null). in method abstractdao.findall() have null in clazz variable. why? please help.
sorry english , formulation. i'm new in site, spring , english
you overcomplicating things. because using spring boot possible create generic interface extends crudrepository , add methods need , not present in there.
take here https://docs.spring.io/spring-data/data-commons/docs/1.6.1.release/reference/html/repositories.html
Comments
Post a Comment