Reputation: 45692
I want to make my entity a singleton. That singleton should be accessible from other entities.
So I decide to set its id manyally, like:
@Entity
@Table(name = "tableName")
public class SingletonEntity {
@Id
private int id = this.getClass().getSimpleName(); //IS IT POSSIBLE? HOW?
//.......
}
Question:
How to achieve that? Please. give me an example.
EDIT:
It is worth to say that my SingletonEntity
has only final string state. So I can say that its stateless. Actually, I don't want a singleton, I only want to set final id to that entity, and I want that id should be equals to SingletonEntity.class.getSimpleName()
For @Balaji Reddy:
I've tried:
@Entity
@Table(name = "Client")
public class Client implements Serializable {
@Id
@Column(name = "id")
private static final String id = Client.class.getSimpleName();
.................
}
And get:
No identifier specified for entity: db.Client
in string
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Upvotes: 2
Views: 1188
Reputation: 7193
Did you try using a constant?
private static final String CLASS_NAME = SingletonEntity.class.getSimpleName();
@Id
private String id = CLASS_NAME;
This is the initialization of the field of your entity, you can also do this in the constructor.
private SingletonEntity() {
this.id = this.getClass.getSimpleName();
}
Upvotes: 1
Reputation: 5700
@Entity
@Table(name = "tableName")
public class SingletonEntity {
@Id
private static int id = this.getClass().getSimpleName(); //try something like this.
//.......
}
Check EJB Specifications 3.1
Upvotes: 1