Spring GCP Datastore interface: setting the namespace? - spring

I want to save an entity into Google Datastore using the Spring DatastoreOperations interface (com.google.cloud.spring.data.datastore.core.DatastoreTemplate.save() method). However, I also need to specify the datastore namespace, as I my data is stored in a separate namespace for each user. Is this at all possible using the Spring abstraction? It works when I hardcode the namespace in application.properties (spring.cloud.gcp.datastore.namespace=...) but obviously I need to set it at runtime depending on the request.

Ok, I think I found the solution: implementing a bean of type DatastoreNamespaceProvider seems to address exactly this need.

Related

Spring boot jpa entity table name from property file

We are working on a spring boot library to generate and validate OTP. It uses database to store the OTP.
We are using Spring Data JPA for Database operations, as it will be easy to handle multiple database systems according to the project.
Now we have ran in to a problem, most of our projects uses Oracle with a single database.
When using the the same lib in multiple projects there is a name conflict.
So we want the name of the OTP table to be configurable using a property file.
We tried #Table(name = "${otp-table-name}") But its not working.
We did a lots of research and found out the hibernate naming strategy configuration can help.
But we dont want to use lots of configuration in our library as we need the library to be easily usable in the projects.
Can someone help us on this aspect.
Thanks in advance.
You can dynamically determine the actual DataSource based on the current context, use Spring's AbstractRoutingDataSource class. You could write your own version of this class and configure it to use a different data source based on the property file.
This allows you to switch between databases or schema without having to change the code in your library.
See: https://www.baeldung.com/spring-abstract-routing-data-source
Using a NamingStrategy is good approach.
You could let it delegate to an existing NamingStrategy and add a prefix.
Use a library specific default for the prefix, but also allow users of your library specify an alternative prefix.
This way your library can be used without extra configuration, but can also handle the case of multiple applications using it in the same database schema.
Of course this might involve the risk of someone using the default prefix without realizing that, that is already used.
It is not clear what the consequences of that scenario are.
If the consequences are really bad you should drop the default value and require that a project specific prefix is used.
When no prefix is specified throw an exception with an instructional error message telling the user, i.e. the developer how to pick a prefix and where to put it.

What's the better way to design a config file in Spring Boot?

I am doing a authorization in Spring Boot. I need a config file to save allowed group for each service. When Spring run, these data will be loaded in cache. Saved in things like
Map<String_serviceName,Set<String_allowedGroup>>
I have a naive method in mind, create a config.properties. Save these in format like:
my.service.service_1=group_1,group_2,group_3...
my.service.service_2=group_1,group_2,group_3...
...
Is there any better way? Or it's enough for this need.
This will work no doubt.
For better readability of your config you may consider using a yaml.
Although, below are my suggestions if you haven't tried them already
user application-.yaml for have env specific configs Use
spring's #ConfigurationProperties to map the entries directly to the objects
(if possible) Use a DB table to store the configs

Why while using Spring Boot I need entities to be serializable?

Simple Spring Boot entity CRUD operations work and do not implement the serializable interface. Almost every example and tutorial is not doing this and still these examples work well. But the official simple code samples use it.
Why is this interface needed and what is the simplest exemplary use case when implementing serializable interface is required?
You don't have to. But looking at the JPA JSR-220 spec there are cases you need to. From the spec:
If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the entity class must implement the Serializable interface.
So if you are passing your object via RMI then you will need to implement Serializable. I can also imagine that Serializable might be needed if you are putting your Entities inside the Session which might be serialized to disk on shutdown of the server.

ClassBridge called by MassIndexer and access to spring service

I wonder whether it is possible to use any spring services from within hibernate search ClassBridge.
Abstract:
My entity does not have all information i'd like to put into indexed document. I can not get it by #IndexedEmbeded too. This data comes from external data source, and I have service to provide this. This is only needed when reindexing. When indexing single object save service provide this information in transient entity field.
For reindex I use MassIndexer.
My application is working in Spring MVC environment. I use annotation driven configuration. So to access my service I need only use #Autowired annotation and service is ready to be used.
Solution?
When using Hibernate Search life is not so easy (or I have no idea how it could be).
To get additional information I decided to use ClassBridge. I also implement simple MassIndexer procedure called from within my spring service.
In my ClassBridge spring does not autowire service. When I try do quick workaround and use static field in ClassBridge and pass service reference from MassIndexer caller the other problem occurred. Service reports exception "no session" (sessionFactory.getCurrentSession() throws exception)... I have no idea where to go further.
What is recommended way to access Spring service from within ClassBridge code?
How can I get active hibernate Session (or SessionFactory) from within ClassBridge?
Thanx for you time & hope your help.
The recommended way is through compile time weaving and #Configurable
A comment on this page (http://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/) provides a much simpler way of handling this that might be useful. You can have Spring autowire an object for you, more explicitly by calling:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
Doc: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

struts2: accessing external service from type converter

is it possible to inject a service reference into custom type converter?
my situation is quite typical in fact, I have a combo, which binds to collection of entities. On submit I get only an ID of selected entity and have to refetch the real object in my action. I was thinking about more elegant way to do this, and it seems like making an ID-to-entity custom converter which would perform fetching - would be a good idea.
But I failed trying to map a converter to Spring bean in the same fashion like actions...
Interesting question. Are you using the spring plugin ?.
It is supposed to take care of service-objects creation, (and wiring with other services) for Struts2, and this should be able to include Type Converters. From here:
By using the struts2-spring-plugin in conjunction with type conversion, developers easily can use dependency injection to provide a converter with services
But I have not used that feature.

Resources