In java Spring and under MongoDB i am creating the following field (id) of a class :
#Id
public String _id;
in order to create the identifier field with the name _id but i get an indentifier id with the compilation warning : WARN 11916 --- [ main] o.s.d.m.c.m.BasicMongoPersistentProperty : Customizing field name for id property not allowed! Custom name will not be considered!
My Question is how to change the name of the id field in java spring (if that's even possible)
EDIT : i tried with #Field("_id") but it's not working.
Thank you,
TL;DR
The warning comes from MongoDB which is unhappy of an #Id field having a property name other than those supported by spring data based on the root cause
Based of the supported Id fields names stored in the Set SUPPORTED_ID_PROPERTY_NAMES includes {"id", "_id}.
That being said your naming of id field should be accepted which is apparently not. It may come from another document. You should check how many #Document you have.
Usually, a basic mongoDB document in Spring includes two main information:
The #Document annotation in the model object (e.g a User)
The #Id annotation from the id property in the model class
Here an example :
#Document
public class User {
#Id
private BigInteger id;
// other fields
/**
* Returns the identifier of the document.
*
* #return the id
*/
public BigInteger getId() {
return id;
}
// getters & setters for other fields (id have only a getter /!\)
}
What you described in your question description is more convenient for JPA based entity for SQL-databases and not suitable for MongoDB document.
Also, I encourage you to check Spring Data MongoDB project and this sample project to see "best practice to start with" when creating such projects
Related
consider an entity with just an id and text field:
#lombok.Data
class Entity {
#javax.persistence.Id
UUID id;
String name;
}
consider that the table definition is as follows:
create table entity (
id uniqueidentifier not null primary key default newid(),
name varchar(max)
);
I am then curious why this doesn't work and how i could make it work:
UUID savedId = entityRepository.save(new Entity().setName("entity name")).getId();
In JPA, entity IDs can be either assigned by the application code, or generated (by the JPA provider, such as Hibernate, or by the database). In many situations, it's desirable to have the entity IDs be generated instead of applicaiton-assigned; it seems like that's what you are expecting.
If so, you need to annotate the id field with #GeneratedValue. For example:
class Entity {
#Id
#GeneratedValue
UUID id;
String name;
}
Note that there are considerations to be made regarding the generation strategy, so you'll want to educate yourself about them and make the right choice based on your situation. This is a good page that discusses the options. This SO Answer also is worth reading (the author is a well-known expert on JPA and Hibernate).
I would like Hibernate to disable certain classes from being validated on startup.
My particular use-case:
spring.jpa.hibernate.ddl-auto=validate
#Table (name = "SAME_TABLE")
public class Entity1 {
#Column
private Long value;
// rest of values
}
#Table (name = "SAME_TABLE")
public class SearchEntity2 {
#Column
private String value;
// rest of values
}
As you can see I have two classes mapped to the same table called SAME_TABLE. This is because I want to do wildcard searches on numeric field value
JPA Validation fails on Oracle (h2 succeeds suprisingly) because it detects that the String is not NUMERIC(10).
This question here by #b0gusb provides an excellent way of filtering out via table name:
How to disable schema validation in Hibernate for certain entities?
Unfortunately my table name is identical. Is there any way of getting to the Java class name from SchemaFilteror perhaps another way of doing this?
Thanks
X
I have a class that looks like this
#Data
#NodeEntity
public class StoryCharacter {
#Index(unique = true)
private String agnosticId;
private String name;
#Relationship(type = "FAMILIAR_WITH")
private Set<StoryCharacter> acquaintances;
}
I needed a custom ID that is not related to the default long id. So I introduced a field and set it as index.
But how to find the object by that id?
I wanted to do it like this
session.openSession().load(StoryCharacter.class, "custom_id")
but it fails with error that it must be Long. I assume that maybe I need to use Filter object for search by that id. Or is there another way?
If you want to use a custom id the field has to be annotated with #Id instead of #Index(unique=true). In cases you do not want to set the id manually, there is an option to provide a id generation strategy (more details in the documentation.
You are seeing this error because Neo4j-OGM cannot determine what type your id field has and falls back to the standard Long. If you define your id as mentioned above, the load will work.
For example, I have an entity below.
#Entity
public class Indexer
#NotNull #Id
private long id;
#Column
private string volumeKey;
}
I want to create a table with a ‘volumeKey’ property in this entity.
For example, A indexer has a ‘X12372’ as a volumeKey of property. I want this entity to be mapped to ‘INDEXER_X12372’.
And I tried to create custom NamingStrategy class for Indexer. And I can’t get an entity to be mapped in this class for making a table of name from.
You want the table to be used to be determined by a value of a property.
This is not possible with JPA or Spring Data JPA.
But some (many?) databases can do this transparently with partitioned tables.
See https://docs.oracle.com/cd/B28359_01/server.111/b32024/partition.htm for Oracle documentation as an example.
It should be easy enough to find a similar document for the database you use.
I have a problem with my code (obviously) and after many searches on Internet, I don't find an answer to my problem, so I ask my question here.
I have this :
#Entity
public class Resident
{
/** Attributes */
#EmbeddedId
private IdResident idResident;
...
#Embeddable
public class IdResident {
#Column(name="NOM")
private String nom;
#ManyToOne
#JoinColumn(name="CODE")
private Port port;
...
#Entity
public class Port
{
/** Attributes */
#Id
#Column(name="CODE")
private String code;
#Column(name="NOM")
private String nom;
...
And I'm using Maven, I've write this in my persistence.xml :
<class>beans.Port</class>
<class>beans.Resident</class>
But when i run the program, no matter what i've write, I have this :
Exception Description: The mapping [port] from the embedded ID class
[class beans.IdResident] is an invalid mapping for this class. An embeddable class that
is used with an embedded ID specification (attribute [idResident] from the source
[class beans.Resident]) can only contain basic mappings. Either remove the non
basic mapping or change the embedded ID specification on the source to be embedded.
I don't see where is my mistake, I think it's because of the IdResident class wich has an Entity object in it, but I don't know how to fiw it
Error message you get explains it quite well, Embeddable that is used as an embedded id can contain only basic mappings, not relationships. In JPA 2.0 specification this is told with following words:
Relationship mappings defined within an embedded id class are not
supported.
Just define attributes that are part of composite id in embeddable that is used as embedded id, and map relationships in entity itself (or in another embeddable and include mappings with #Embedded).
In my opinion this is based on the ManyToOne mapping in the IdResident class cause the error message pushs me into this direction.