Get String from SQL function, no entity, kotlin - oracle

Im confused..
I need to use Oracle function to retrieve just a single String, No entity.
So i try to use CrudRepository, but that dosent work without Entity?
interface UserRepository : CrudRepository<Contact, String>{
#Query(nativeQuery = true, value = "SELECT schema.get_user(:id) FROM DUAL")
fun getUser(id: String): String?
}
or
interface UserRepository : CrudRepository<Contact, String>{
#Query(nativeQuery = true, value = "SELECT schema.get_user(:id) AS name, '' AS email FROM DUAL")
fun getUser(id: String): String?
}
where i try to name returning string to fit entity i seem to be forced to use.
Entity Contact is super simple entity with two attributes.
#Entity
data class Contact(
#Id
val name: String?,
val email: String?
)
Function will only ever return one string, but i cant figure how would i insert that as a name.. With this Query i get ORA-00904: : invalid identifier most likely because i have no idea how to match identifier returned from function.. (SQLdeveloper gives function name as column name but the returned string dosent really have ANY column name, since it is calculated...)
I can't do a repository for a String either, can i?
EDIT: trying to clarify what i need:
Entity has two attributes, just one comes from the Query including function, other one must be fetch from other side of the world. How do i run this query and return either single string or entity (with email as null) only have gotten array of errors so far, thanks

Related

Spring Open Projection - Object and Additional Value

Let's assume we have a model like this:
class User {
String name;
Integer age;
}
The query returns results, and the extraField is mapped, but the user isn't.
I have a query that joins the User table with another table and returns the User object and one more field that comes from this other table.
Is it possible to define an open projection in Spring that would map the return values from the query? I guess it should look something like the following, but I can't get it to work.
interface UserProjection {
User getUser();
#Value("#{target.extrafield}")
String getExtraField();
}

How to gracefully transform entity into DTO in Kotlin?

I am working on Kotlin + SpringBoot web service, in which I want to transform DTOs into entities in the most convenient way.
Entities:
#Entity
data class Shop(
#Id
#GeneratedValue
val id: Long,
val name: String
#OneToOne
val owner: User,
...
)
#Entity
data class User(
#Id
#GeneratedValue
val id: Long,
val name: String,
...
)
DTO:
data class ShopDTO(
val id: Long,
val name: String,
val ownerId: Long,
val ownerName: String,
...
)
So when someone wants to create a new Shop, my service gets a ShopDTO(name, ownerId) as request body, then I need to transform it into Shop object to be able to save it to the DB. Now here is how my mapper function looks like:
fun fromDTO(source: ShopDTO) = Shop(
id = source.id,
name = source.name,
owner = ???,
...
)
To be able to store a Shop with an owner I only need an id. It would be enough to create a new User with the given ownerId.
To achive this I tried these solutions:
Add default value to the fields in the User class.
Make the fields nullable.
Add a secondary constructor. This also needs default values.
Use some reflection magic to create an empty object and then set the id.
Call a findById method on the UserRepository with the given id.
I want to keep the non-null, immutable fields of my entities and do not want to use reflection. Also do not want to run an unnecessary select DB query just to get back the user by the id.
Could you please suggest me other options? How would you handle this situation? Is there any good mapper framework in Kotlin which can solve this problem?
Firstly, your question says you want to do entity -> DTO, but actually you want to do DTO -> entity, so you should clear that up.
Secondly, you are getting the shop name and owner Id in the ShopDTO. But you are assigning the owner Id to the shop Id in the your fromDTO(source: ShopDTO) function. Changing it up would be sufficient.
fun fromDTO(source: ShopDTO) = Shop(
name = source.name,
owner = ownerRepo.findById(source.ownerId)
)
Obviously, if you're using JPA, then you have to make a DB call to get the owner first. If your business logic doesn't ensure that a User with that Id exists, then you could write a method like this to make a user.
fun getOrCreateUser(ownerId: Long) =
ownerRepo.findUserById(ownerId) ?: User(
id = ownerId,
name = "Some random DefaultName"
).run(ownerRepo::save)
This would get a User by the Id if it exists, or create a new user with some generic name.
Do let me know if this solves your issue!

SpringBoot named query: IN-clause not working for multiple values

"In clause" not working on passing multiple values
Repository code
#Query( "select myObject from MyObject myObject where myObject.anyColumn in :values" )
public List<MyObject> findPriDestByCntryAbbr(#Param("values") List<String> values);
Calling from service
List<String> values = Arrays.asList(valuesString.split(","));
List<MyObject> result = myObjectRepository.findByAnyColumns(values);
When i am passing single value it is retrieving correct information from table, But on passing multiple values in List "values" giving empty result
Ideally it should work. You should check the values variable.
The other way you can try is- without writing query.
As per the doc we can create MyObjectRepository interface by extending Repository interface and then have a method to fetch all MyObjects. It goes like:
public interface MyObjectRepository extends Repository<MyObject,**Long**> {
List<MyObject> findBy**AnyColumn**In(Collection<String> values);
}
"AnyColumn" is your column name and second parameter for Repository interface is as per your requirement.

A Jpa query annotation to return results as key value pairs

I have a users table and I am trying to use annotated Queries in spring boot to get a result set. I am able to get result set as a list, but that does not have field names. How do I get the result set with field name as key and value pairs?
Current response [[1,"Jay"]] , what I want to do is {"id":1,"Name":"Jay"}
-----Here is my repository class-----
#Repository
public interface UsersauthRepository2 extends JpaRepository<Users2,Long> {
#Query("select id,name,email from Users u where LOWER(email) = LOWER(:email) and LOWER(u.password) = LOWER(:password)")
List<Users2> querybyemail(#Param("email") String email,#Param("password") String password);
}
The request doesn't return fields names.
If you need to get them :
You have them already as method argument
You need to use reflection.
Good luck

Spring Data Mongo nested id won't work

Does Spring Data treats mongo nested "id" attributes differently? I explain my problem: I have collection matches with the following structure
"teams": [
{
"id" : "5601",
"name" : "FC Basel"
},
... // more
]
When I want to retrieve all the matches which has team id 5601 I execute the following query
db.matches.find({ "teams.id" : "5601"})
Which works perfectly and returns some objects.
When I make a method
public List<MatchMongo> findByTeams_id(String id);
on my MatchRepository interface I get 0 results back while there are.
Logs shows
Created query Query: { "teams.id" : "5601"}, Fields: null, Sort: null
find using query: { "teams.id" : "5601"} fields: null for class: class
MatchMongo in collection: matches
So the query he makes seems to be the right one... :S
Trying with other fields (referee.name for ex.) works.
I even tried with the #Query annotation, but can't get it to work
Is there another solution? Is this a bug or am I doing something wrong?
Oh found the solution:
MatchMongo had List<TeamMongo> teams; on whereI had
#Id
private String id;
#Field(value = "id")
private String teamIdAttr;
So the method should be called
public List<MatchMongo> findByTeams_teamIdAttr(String id);
Never thought the method name should reflect objects attributes instead of collection structure
Thanks #martin-baumgartner your comment helped to solve this :)

Resources