Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringData Mongo @Column equivalent annotation (@Property?)

Is there a SpringData Mongo equivalent of the JPA @Column annotation?

Basically, I've got a POJO with a property that I want to store in Mongo with a different name. So, the following object:

public class Pojo{
    @Property("bar")
    private String foo = "Hello World";
}

would be persisted as:

{
    "_class":"com.example.Pojo",
    "bar" : "Hello World"
}

Note: I don't want to use the MappingMongoConverter to explicitly do it

like image 758
David Welch Avatar asked Mar 26 '13 02:03

David Welch


People also ask

What is @column annotation in spring boot?

@Column annotation is used for Adding the column the name in the table of a particular MySQL database. Syntax: @Column(name=”DESC”, nullable=false, length=512) public String getDescription() {

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.

Which annotation is used to link a MongoDB document with a spring bean?

The @Id annotation tells the mapper which property you want to use for the MongoDB _id property and the @Indexed annotation tells the mapping framework to call ensureIndex on that property of your document, making searches faster.

What is @entity annotation in spring boot?

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.


2 Answers

The Spring Data reference documentation lists @Field as the annotation to customize the key and ordering of properties mapped to a MongoDB document.

like image 138
Oliver Drotbohm Avatar answered Oct 10 '22 21:10

Oliver Drotbohm


You can use @Field

It lets you define custom key name in DB and the insert order

like image 35
Ori Dar Avatar answered Oct 10 '22 22:10

Ori Dar