I am trying to check the hibernate annotation Column :
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Column {
...
}
Getting the class using the object doesn't work using this code
Object o = an object ...
Class oClass = o.getClass();
for (Annotation a : oClass.getDeclaredAnnotations() ) {
System.out.println ("\t * Annotation : " + a.annotationType().getSimpleName());
}
for(Method method : oClass.getMethods()){
System.out.println(" method =>" + method.getName());
for (Annotation a : method.getAnnotations()) {
System.out.println("\t * Annotation : " + a.annotationType().getSimpleName());
}
}
If I use directly the class MyObject.class instead of myInstance.getClass() it works, I don't understand why, the retention is RUNTIME so it should work ? What am I missing ?
here is the class, ps I removed the fields declaration
@Entity
@Table(name="ticket"
)
public class Ticket implements java.io.Serializable {
public Ticket() {
}
public Ticket(Integer id, ... same for all fields) {
this.id = id;
... same for all fields
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="created_by")
public User getUserByCreatedBy() {
return this.userByCreatedBy;
}
public void setUserByCreatedBy(User userByCreatedBy) {
this.userByCreatedBy = userByCreatedBy;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="event_id")
public Event getEvent() {
return this.event;
}
public void setEvent(Event event) {
this.event = event;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="work_user")
public User getUserByWorkUser() {
return this.userByWorkUser;
}
public void setUserByWorkUser(User userByWorkUser) {
this.userByWorkUser = userByWorkUser;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="assigned_to")
public User getUserByAssignedTo() {
return this.userByAssignedTo;
}
public void setUserByAssignedTo(User userByAssignedTo) {
this.userByAssignedTo = userByAssignedTo;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="associated_ticket")
public Ticket getTicket() {
return this.ticket;
}
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="work_organization")
public Organization getOrganizationByWorkOrganization() {
return this.organizationByWorkOrganization;
}
public void setOrganizationByWorkOrganization(Organization organizationByWorkOrganization) {
this.organizationByWorkOrganization = organizationByWorkOrganization;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="category")
public OrganizationReference getOrganizationReference() {
return this.organizationReference;
}
public void setOrganizationReference(OrganizationReference organizationReference) {
this.organizationReference = organizationReference;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="power_station_id")
public PowerStation getPowerStation() {
return this.powerStation;
}
public void setPowerStation(PowerStation powerStation) {
this.powerStation = powerStation;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="organization_id")
public Organization getOrganizationByOrganizationId() {
return this.organizationByOrganizationId;
}
public void setOrganizationByOrganizationId(Organization organizationByOrganizationId) {
this.organizationByOrganizationId = organizationByOrganizationId;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="contract_id")
public ServiceContract getServiceContract() {
return this.serviceContract;
}
public void setServiceContract(ServiceContract serviceContract) {
this.serviceContract = serviceContract;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="default_instance_id")
public DefaultInstance getDefaultInstance() {
return this.defaultInstance;
}
public void setDefaultInstance(DefaultInstance defaultInstance) {
this.defaultInstance = defaultInstance;
}
@Column(name="type")
public Integer getType() {
return this.type;
}
public void setType(Integer type) {
this.type = type;
}
@Column(name="workflow_id")
public Integer getWorkflowId() {
return this.workflowId;
}
public void setWorkflowId(Integer workflowId) {
this.workflowId = workflowId;
}
@Column(name="ticket_number")
public Integer getTicketNumber() {
return this.ticketNumber;
}
public void setTicketNumber(Integer ticketNumber) {
this.ticketNumber = ticketNumber;
}
@Column(name="status")
public Integer getStatus() {
return this.status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Column(name="severity")
public Integer getSeverity() {
return this.severity;
}
public void setSeverity(Integer severity) {
this.severity = severity;
}
@Column(name="priority")
public Integer getPriority() {
return this.priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
@Column(name="title", length=150)
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="details", length=65535)
public String getDetails() {
return this.details;
}
public void setDetails(String details) {
this.details = details;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="created_date", length=19)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="update_date", length=19)
public Date getUpdateDate() {
return this.updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
@Column(name="assigned_type", length=45)
public String getAssignedType() {
return this.assignedType;
}
public void setAssignedType(String assignedType) {
this.assignedType = assignedType;
}
@Column(name="spent_hours", precision=22, scale=0)
public Double getSpentHours() {
return this.spentHours;
}
public void setSpentHours(Double spentHours) {
this.spentHours = spentHours;
}
@Column(name="cost", precision=22, scale=0)
public Double getCost() {
return this.cost;
}
public void setCost(Double cost) {
this.cost = cost;
}
@Column(name="currency", length=3)
public String getCurrency() {
return this.currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
@Column(name="ticket_quote_status")
public Integer getTicketQuoteStatus() {
return this.ticketQuoteStatus;
}
public void setTicketQuoteStatus(Integer ticketQuoteStatus) {
this.ticketQuoteStatus = ticketQuoteStatus;
}
@Column(name="ticket_account_status")
public Integer getTicketAccountStatus() {
return this.ticketAccountStatus;
}
public void setTicketAccountStatus(Integer ticketAccountStatus) {
this.ticketAccountStatus = ticketAccountStatus;
}
@Column(name="maintainer_invoice_ref", length=64)
public String getMaintainerInvoiceRef() {
return this.maintainerInvoiceRef;
}
public void setMaintainerInvoiceRef(String maintainerInvoiceRef) {
this.maintainerInvoiceRef = maintainerInvoiceRef;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="work_term_date", length=19)
public Date getWorkTermDate() {
return this.workTermDate;
}
public void setWorkTermDate(Date workTermDate) {
this.workTermDate = workTermDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="work_start_date", length=19)
public Date getWorkStartDate() {
return this.workStartDate;
}
public void setWorkStartDate(Date workStartDate) {
this.workStartDate = workStartDate;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="work_end_date", length=19)
public Date getWorkEndDate() {
return this.workEndDate;
}
public void setWorkEndDate(Date workEndDate) {
this.workEndDate = workEndDate;
}
@Column(name="solved")
public Boolean getSolved() {
return this.solved;
}
public void setSolved(Boolean solved) {
this.solved = solved;
}
@Column(name="resolution", length=65535)
public String getResolution() {
return this.resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<TicketHistory> getTicketHistories() {
return this.ticketHistories;
}
public void setTicketHistories(Set<TicketHistory> ticketHistories) {
this.ticketHistories = ticketHistories;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<DeviceTransmissionHasDefectDetection> getDeviceTransmissionHasDefectDetections() {
return this.deviceTransmissionHasDefectDetections;
}
public void setDeviceTransmissionHasDefectDetections(Set<DeviceTransmissionHasDefectDetection> deviceTransmissionHasDefectDetections) {
this.deviceTransmissionHasDefectDetections = deviceTransmissionHasDefectDetections;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<StockUsage> getStockUsages() {
return this.stockUsages;
}
public void setStockUsages(Set<StockUsage> stockUsages) {
this.stockUsages = stockUsages;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<TicketComment> getTicketComments() {
return this.ticketComments;
}
public void setTicketComments(Set<TicketComment> ticketComments) {
this.ticketComments = ticketComments;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<TicketProperty> getTicketProperties() {
return this.ticketProperties;
}
public void setTicketProperties(Set<TicketProperty> ticketProperties) {
this.ticketProperties = ticketProperties;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<Notification> getNotifications() {
return this.notifications;
}
public void setNotifications(Set<Notification> notifications) {
this.notifications = notifications;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="ticket_has_tag", joinColumns = {
@JoinColumn(name="ticket_id", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="tag_id", nullable=false, updatable=false) })
public Set<Tag> getTags() {
return this.tags;
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="ticket_has_ticket_tag", joinColumns = {
@JoinColumn(name="ticket_id", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="tag_id", nullable=false, updatable=false) })
public Set<Tag> getTags_1() {
return this.tags_1;
}
public void setTags_1(Set<Tag> tags_1) {
this.tags_1 = tags_1;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<TicketHasEscalation> getTicketHasEscalations() {
return this.ticketHasEscalations;
}
public void setTicketHasEscalations(Set<TicketHasEscalation> ticketHasEscalations) {
this.ticketHasEscalations = ticketHasEscalations;
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="ticket_has_document", joinColumns = {
@JoinColumn(name="ticket_id", nullable=false, updatable=false) }, inverseJoinColumns = {
@JoinColumn(name="document_id", nullable=false, updatable=false) })
public Set<Document> getDocuments() {
return this.documents;
}
public void setDocuments(Set<Document> documents) {
this.documents = documents;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<CheckList> getCheckLists() {
return this.checkLists;
}
public void setCheckLists(Set<CheckList> checkLists) {
this.checkLists = checkLists;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<Ticket> getTickets() {
return this.tickets;
}
public void setTickets(Set<Ticket> tickets) {
this.tickets = tickets;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<Task> getTasks() {
return this.tasks;
}
public void setTasks(Set<Task> tasks) {
this.tasks = tasks;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="ticket")
public Set<DownTime> getDownTimes() {
return this.downTimes;
}
public void setDownTimes(Set<DownTime> downTimes) {
this.downTimes = downTimes;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name="scheduled_date", length=19)
public Date getScheduledDate() {
return this.scheduledDate;
}
public void setScheduledDate(Date scheduledDate) {
this.scheduledDate = scheduledDate;
}
}
edit 1 : Ok so I found something strange, if I load my object using org.hibernate.Session.load() method
Integer anId = 1;
Ticket ticket = (Ticket)session.load(Ticket.class,anId);
Loading the object this way and using reflection miss annotations, but if I try to invoke a new Object using
Ticket ticket = new Ticket();
works ...
In short :
Class oClass = session.load(Ticket.class,anId).getClass();
miss annotation with reflection but
Class oClass = new Ticket().getClass();
works
edit 2 :
thx dan1st, hibernate generate a subclass of Ticket when calling the method session.load
edit 3 :
so in my case I need to get the super class
Class oClass = session.load(Ticket.class,anId).getClass().getSuperclass()