Is it possible to create an entity without #Entity annotation.
I have an employee class which is not annotated with #Entity.
Question:
How entity class is identified by JPA?
Is it possible to use custom annotation let say #MyOwnEntity instead of #Entity?
Is there is a way I can override JPA method to indicate package(com.mt.own.example) type are containing entity class? Note: I am going to annotate all the class within (com.mt.own.example) package with my own annotation.
i dont know why would you do that but yes you can ...
#Documented
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
public #interface MyOwnEntity {
String name() default "";
}
Related
I use the javax.validation and have a Hibernate validator on a classpath.
I would like to validate entities of a specific class on the class level (not fields level).
I can do this with by creating a custom class-level annotation and a paired ConstraintValidator as shown below:
#Constraint(validatedBy = CustomValidator.class)
#Target({ ElementType.TYPE })
public #interface CustomConstraintAnnotation {
}
class CustomValidator
implements ConstraintValidator<CustomConstraintAnnotation, Object> {
}
However, I would like to avoid creating a custom annotation and assign a custom validator per entity type, so that the validator would be registered for this type and validate instances of this type by default when validator.validate(entity) is invoked without having to annotate the type.
The javax.validation.ConstraintValidator does not fit, because it works only in pair with an annotation.
Basically, I would like to create a mapping between the type and the corresponding custom validator without using an annotation.
Is there a way to achieve this?
i need to pass the schema name dynamically to the entity
#Entity
#Table(name = "customer_table" schema="${dynamicSchemaName}")
#JsonIgnoreProperties
public class Customer implements Serializable {
//....
}
how can i replace ${dynamicSchemaName} with the schema that will be picked by the code where this entity is called?
The simple answer is that you can't. Although there are hacks and ways to inject dynamic data into an annotation, that doesn't exist for Hibernate. Annotations are defined at compile time and read at run time.
E.g., will the below code run properly?
Or do I have to define a separate Entity Listener for each and every entity class?
#Entity
#EntityListeners(value = Abc.class)
public class Abc{
...
#PreUpdate
public void doPreUpdate(){
//do something
}
...
}
You don't even need the #EntityListeners annotation. It will work just fine with just the #PreUpdate annotated method.
From javadoc for #PreUpdate:
This annotation may be applied to methods of an entity class, a mapped
superclass, or a callback listener class.
I want the annotation below to be the default for all of my domain objects.
#JsonInclude(Include.NON_NULL)
Rather than adding this manually to every domain class, I was wondering if there is a way to make this the default for all domains.
I am working with spring-boot application that utilizes Gradle for builds and JPA and Hibernate for persistence.
Java supports way to inherit annotation of super type if the annotation applied to super type is annotated with a meta annotation #Inherited doc.
But since the #JsonInclude annotation is not annotated with #Inherited, I think you'll have to add it manually to your domain classes.
You can make one SuperClass annotate the same with #JsonInclude(Include.NON_NULL) and extend it, like below:
#JsonInclude(Include.NON_NULL)
public class SuperClass {
}
public class SubClass extends SuperClass{
}
In Java, is there a way to change the behaviour of an annotation depending on the type of the annotated field?
I know that annotation presence is supposed to be tested by code. Not the opposite. But the case is rather particular: this is a Jackson 2.0 « inside » annotation which gather a list of annotations. We use it to define the field name (#JsonProperty) and the field serializing policies (#JsonSerialize).
The serialisation policies must be adapted to the annotated field. And, because we are talking of a framework, one unique annotation is far better than two separate ones.
#Retention(RUNTIME)
#JacksonAnnotationsInside.
#JsonProperty("_id")
#JsonSerialize(using=IdSerializer.class)
#JsonDeserialize(using=IdDeserializer.class)
public #interface Id {}
Some cases need to turn the serializers down, that's the point. In the following example, the String must be processed by the de/serializers, ObjectId don't. Both need to be renamed _id by the #JsonProperty.
public class Car {
#Id String id
}
public class Bus {
#Id ObjectId id
}
Any clues?