2

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");
3
  • @Alberto Solano, yes I've read ). I've implemented a State Pattern. Each of my states is a singleton. I can make them a spring beans, but there is a little problem. Each of those states has it own state (final in my case). So if I make them a spring beans I need to recompiling my app each time I want to change their state. I see the solution in making them a singleton entities. See also stackoverflow.com/questions/20091168/… Commented Nov 20, 2013 at 9:26
  • I don't understand exactly what you are asking. Perhaps you should take a look at IOC frameworks that manage singletons way better than manually done with static fields / functions. I think you should change your question about: initialization of field of entity by the class simple name. Commented Nov 20, 2013 at 11:17
  • I have Spring's singleton beans. I look for correct way to replace those beans with hibernate entities. Commented Nov 20, 2013 at 11:19

2 Answers 2

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@NickoleAbs for the same id, the same columns will be updated in your datastore, if it is not the case check your mapping + database. You should not worry about that, primary keys are by definition unique. Maybe you are looking for disabling rewriting of an existing entity which is a different case.
1
@Entity
@Table(name = "tableName")
public class SingletonEntity {

  @Id
  private static int id = this.getClass().getSimpleName(); //try something like this.

  //.......
}

Check EJB Specifications 3.1

2 Comments

Does it work for you? I can try only after a large refactoring; just want to be certain about that. And what about final? I want to be certain that noone can store in DB two entities of one class .
It doesn't work for me. Look at 'For @Balaji Reddy' section of my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.