Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8480247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:14:40+00:00 2026-06-10T19:14:40+00:00

How does annotation work with Java? And how can I create custom annotations like

  • 0

How does annotation work with Java? And how can I create custom annotations like this:

@Entity(keyspace=':')
class Student
{
  @Id
  @Attribute(value="uid")
  Long Id;
  @Attribute(value="fname")
  String firstname;
  @Attribute(value="sname")
  String surname;

  // Getters and setters
}

Basically, what I need to have is this POJO be serialized like this when persisted:

dao.persist(new Student(0, "john", "smith")); 
dao.persist(new Student(1, "katy", "perry"));

Such that, the actual generated/persisted object is a Map<String,String> like this:

uid:0:fname -> john
uid:0:sname -> smith
uid:1:fname -> katy
uid:1:sname -> perry

Any ideas how to implement this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:14:41+00:00Added an answer on June 10, 2026 at 7:14 pm

    If you create custom annotations you will have to use Reflection API Example Here to process them.
    You can refere How to declare annotation.
    Here is how example annotation declaration in java looks like.

    import java.lang.annotation.*;
    
    /**
     * Indicates that the annotated method is a test method.
     * This annotation should be used only on parameterless static methods.
    */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Test { }
    

    Retention and Target are known as meta-annotations.

    RetentionPolicy.RUNTIME indicates that you want to retain the annotation at runtime and you can access it at runtime.

    ElementType.METHOD indicates that you can declare annotation only on methods similarly you can configure your annotation for class level, member variable level etc.

    Each Reflection class has methods to get annotations which are declared.

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
    Returns this element's annotation for the specified type if such an annotation is present, else null.
    
    public Annotation[] getDeclaredAnnotations()
    Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers. 
    

    You will find these methods present for Field, Method,Class classes.

    e.g.To retrieve annotations present on specified class at run time

     Annotation[] annos = ob.getClass().getAnnotations();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does @Version annotation work in JPA? I found various answers whose extract is
What does the the dispatcher attribute of the @Trace annotation for New Relic do?
Using Spring 3, I like to create an exception handler using the ExceptionHandler annotation
I have to admit that I am pretty confused how annotations work in java
I have problem in a JPA entity class. This entity was working perfectly as
Does anyone know if it's possible to have a bundle use the annotation reader
What does the castle validators offer me over standard data annotations? I am a
Maven 2 does not seem to consider my @Test and @Ignore annotations. How do
I am working on an Eclipse plugin which uses custom textmarker annotations. There are
I have webproject on Java EE 5 and Websphere 7.0 I need to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.