How to use MongoAuditting with MongoTemplate? - spring

i'm trying to upsert using MongoTemplate. but auditting does not work.(#CreatedDate, #ModifiedDate fields didn't save) how to enable auditing with MongoTemplate?
i added #EnableMongoAuditting
#Configuration
#EnableMongoAuditing
class MongoConfig
my document class
#Document("sample")
class SampleDocument(
var title: String
) {
#MongoId(FieldType.OBJECT_ID)
var id: String? = null
#CreatedDate
var createdAt: LocalDateTime? = null
#LastModifiedDate
var lastModifiedAt: LocalDateTime? = null
}
and MongoTemplate query
mongoTemplate.upsert(
Query(Criteria.where("title").`is`("Hello")),
Update().set("title", "Hi"),
SampleDocument::class.java
)

Related

Unable to insert entity using room

I am new to android Room and trying to insert some one-to-many relationships into my database. But I am facing some issues that I have not managed to fix sofar.
Here is my data constellation:
Entity:
#Entity(tableName = "artist")
data class Artist(
#PrimaryKey(autoGenerate = true) val id: Long = 0,
val name: String,
) {
}
#Entity(
tableName = "song",
foreignKeys = [ForeignKey(
entity = Artist::class,
parentColumns = ["id"],
childColumns = ["artistId"],
onDelete = ForeignKey.CASCADE
)]
)
data class Song(
#PrimaryKey(autoGenerate = true) val id: Long = 0,
val artistId: Long,
val title: String?
) {
}
data class ArtistWithSongs(
#Embedded val artist: Artist,
#Relation(parentColumn = "id", entityColumn = "artistId", ) val songs: List<Meal>
) {
}
Repository:
#Singleton
class AppRepository #Inject constructor(
private val artistDao: ArtistDao
) {
suspend fun insert(artist: Artist) {
artistDao.insert(artist)
}
}
Dao:
#Dao
interface ArtistDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(artist: Artist)
}
ViewModel:
#HiltViewModel
class ArtistViewModel #Inject constructor(
private val repository: AppRepository,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
fun insert(artist: Artist) = viewModelScope.launch {
repository.insert(artist)
}
}
Then in my activity I call:
private val viewModel: ArtistViewModel by viewModels()
...
val artist = Artist("Bob")
viewModel.insert(artist)
But insert artist does not work. The database is still empty.
Thanks
I was using viewModel.artist.value to check if any artist was present. As the method returning artist has LiveData as return type, I was getting null.
Observing the artist instead proved that my insert method is working just right.

how to include an Embedded attribute to json return object spring boot

I have a Kotlin Spring Boot application with these following entities:
import com.fasterxml.jackson.annotation.JsonIgnore
import javax.persistence.*
#Entity
#Table(name = "users")
class Users(
#Id
var username: String = "",
#JsonIgnore
var password: String = "",
#JsonIgnore
var enabled: Boolean = false,
#ElementCollection
#CollectionTable(name = "authorities", joinColumns = [JoinColumn(name = "username")])
#MapKeyColumn(name = "users")
private var authorities : MutableSet<Authorities> = mutableSetOf()
)
and
#Embeddable
class Authorities(
var authority: String
)
When I call this endpoint:
#GetMapping("/users")
fun getUserById(#RequestParam(value = "username") username : String) : ResponseEntity<Users>{
val optionalUser = usersService.getByUsername(username)
return if(optionalUser.isPresent) ResponseEntity.ok(optionalUser.get())
else ResponseEntity(HttpStatus.BAD_REQUEST)
}
it returns correctly, as you can see in this json:
{
"username": "test#gmail.com"
}
But it doesn't return the list of authorities that belongs to this user, and I wanted to have it returned. My question is: How could I do it?
Thank you so much for helping

Spring boot UUID primary key entity doesn't show right id after created

So I have entity with UUID as primary key. After I create the entity in my service, the response return wrong id.
My Role entity
package denny.study.stock.entity
import denny.study.stock.util.converter.JsonToMapConverter
import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.GenericGenerator
import org.hibernate.annotations.Type
import org.hibernate.annotations.UpdateTimestamp
import java.util.*
import javax.persistence.*
#Entity
#Table(name = "roles")
class Role {
#Id
#GeneratedValue(generator = "uuid2")
#GenericGenerator(name = "uuid2", strategy = "uuid2")
#Column(name = "id", columnDefinition = "CHAR(36)")
#Type(type = "uuid-char")
var id: UUID = UUID.randomUUID()
#Column
var name: String = ""
#Column
var slug: String = ""
#Column
#Convert(converter = JsonToMapConverter::class)
var permissions: MutableMap<String, Boolean>? = null
#Column(name = "created_at")
#CreationTimestamp
var createdAt: Date = Date()
#Column(name = "updated_at")
#UpdateTimestamp
var updatedAt: Date? = null
}
My store method in role service
override fun store(roleRequest: RoleRequest): RoleResponse {
val role = Role().apply {
name = roleRequest.name
slug = roleRequest.slug
permissions = roleRequest.permissions
}
roleRepository.save(role)
return RoleResponse(role)
}
My Role response
package denny.study.stock.model.response.role
import denny.study.stock.entity.Role
class RoleResponse(role: Role) {
var id = role.id
var name = role.name
var slug = role.slug
var permissions = role.permissions
}
the json response return id "f95bddf6-eb22-49bb-b8e6-5eb819603fa9"
{
"code": 200,
"status": "OK",
"data": {
"id": "f95bddf6-eb22-49bb-b8e6-5eb819603fa9",
"name": "Role 1",
"slug": "role-1",
"permissions": {
"asd": true,
"dsa": false
}
}
}
while in DB it stored as "87596692-7ee9-4ecb-a425-3b1372d901f4". Do you know why It return the wrong id? Thank you!
You are using #GeneratedValue annotation and also assigning UUID.randomUUID() to the id attribute. Either use one or the other, not both.
If you want the ID to be generated by the persistence provider then keep #GeneratedValue and remove #GenericGenerator (which is a Hibernate annotation) and UUID.randomUUID(). If you want to do that on your own then remove #GeneratedValue and #GenericGenerator.

Removing relationship between two nodes in Spring Boot 2.4.3 Neo4j

I'm in the middle of upgrading from Spring Boot 2.3 to 2.4 and are having some issues with Neo4j after upgrade.
I'm creating two nodes, and adding a relationship between them
#Node
class Document(
#Id
val id: String,
) {
#Relationship(type = "CONCERNS")
var concerns: Concerns? = null
}
#RelationshipProperties
class Concerns(
#TargetNode val user: User
) {
#Id
#GeneratedValue
private var id: Long? = null
}
#Node
class User(
#Id
val id: String
) {
var givenName: String? = null
var familyName: String? = null
}
Creating the Nodes and adding the relationship works fine
But when I try to remove the relationship from the user, nothing happens:
val document = documentRepository.findById(id)
document.concerns = null
documentRepository.save(document)

#CreationTimestamp and #UpdateTimestamp don't work in Kotlin

This is my Tag and Post Entity classes:
#Entity
class Tag(
#get:NotBlank
#Column(unique = true)
val name: String = "",
val description: String = ""
) {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
val id: Int? = null
#ManyToMany(mappedBy = "tags")
val posts: MutableSet<Post> = mutableSetOf()
#CreationTimestamp
lateinit var createDate: Date
#UpdateTimestamp
lateinit var updateDate: Date
fun addPost(post: Post) {
this.posts.add(post)
post.tags.add(this)
}
}
#Entity
class Post(
#get:NotBlank
var name: String = "",
val content: String = ""
) {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
val id: Int? = null
#ManyToMany
#Cascade(CascadeType.ALL)
val tags: MutableSet<Tag> = mutableSetOf()
#CreationTimestamp
#Temporal(TemporalType.TIMESTAMP)
lateinit var createDate: Date
#UpdateTimestamp
#Temporal(TemporalType.TIMESTAMP)
lateinit var updateDate: Date
fun addTag(tag: Tag) {
this.tags.add(tag)
tag.posts.add(this)
}
}
The query:
val post1 = Post( "Post 1", "Post 1 content");
val post2 = Post( "Post 2", "Post 2 content");
var tag = Tag( "news", "Tag description")
post1.addTag(tag)
post2.addTag(tag)
em.persist(post1)
em.persist(post2)
em.remove(post1)
em.flush()
But then, the createDate and updateDate return null (both tag and post):
I converted this code to Java and it works fine
Kotlin version: 1.2-M2
springBootVersion: '2.0.0.M7'
The problem likely exists in the fact that those annotations are not limited to what they should annotate. This means kotlin does not know exactly where to put them in the final bytecode.
To get the same bytecode as would be generated by java, you need to specify the annotation target of the annotation in question:
#field:CreationTimestamp
lateinit var createDate: Date
The context is not enough to provide any meaningful answer.
You have to give more context, in particular the application server, with its version, or even code for this specific use-case. To start with, I'd suggest first checking if the same Java code works.
My best guess so far, you're using Spring Data JPA while #CreationTimestamp and #UpdateTimestamp are Hibernate specific.

Resources