Ozeta
Ozeta

Reputation: 329

Java Spring initialize hibernate repository one time

I followed the official spring tutorial and I declared my object as follow:

The repository:

@Repository
public interface PropertyRepository extends CrudRepository<Property, Long> {
}

the Property Object:

@Entity
@Table(catalog = "configdb", name = "properties", uniqueConstraints = {@UniqueConstraint(columnNames = {"property_id"})})
public class Property implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "property_id")
    private Long propertyId;
    @Column(name = "property_key", length = 50)
    private String propertyKey;

the application:

@SpringBootApplication
@ComponentScan({"org.demo*"})

@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }

and a really short controller:

@Controller
@RequestMapping(path = "/demo")
public class PropertyController {
    @Autowired
    private PropertyDao propDao;
    public PropertyController(PropertyRepository repo) {
        propDao = PropertyDao.setRepository(repo);
    }
    @GetMapping(path = "/map")
    public @ResponseBody
    HashMap<String,String> get() {
        return propDao.getPropertiesMap();
    }

PropertyDao sets his Repository in the costructor then converts a Property to a HashMap.

I see from the logs that everytime I execute a request on the controller, Hibernate is called and a query to the mysql is executed, like if the PropertyController is created at every request.

However this Property object contains only initials configurations and calling the db at every request is a huge overhead.

Do you have any solution?

EDIT: added PropertyDao

@Component
public class PropertyDao {
    public PropertyDao setRepository(PropertyRepository repository) {...}
    public HashMap<String, String> getPropertiesMap(){...}
}

Upvotes: 1

Views: 441

Answers (1)

lealceldeiro
lealceldeiro

Reputation: 14998

Create a service (services are singleton, they are created only once) and move the logic there. Then call the services methods from the controllers.

Something like this:

@Service
@Transactional
public class MyService {

    @Autowired
    private PropertyDao propDao;

    public MyService(PropertyRepository repo) {
        propDao = PropertyDao.setRepository(repo);
    }


    HashMap<String,String> getPropertiesMap() {
        return propDao.getPropertiesMap();
    }
}

and in your controller:

@Controller
@RequestMapping(path = "/demo")
public class PropertyController {
    @Autowired
    private MyService myService;

    public PropertyController(MyService myService) {
        this.myService = myService;
    }

    @GetMapping(path = "/map")
    public @ResponseBody
    HashMap<String,String> get() {
        return myService.getPropertiesMap();
    }
}

Upvotes: 1

Related Questions