5

My question is, can i use a static logger in a JPA Entity, or will it cause some problem? I would like to do something like this (the logger is log4j):

    @Entity
public class AlertRule implements Serializable {
/**
 * Serial version ID.
 */
private static final long serialVersionUID = 9000392523924653431L;

/**Logger. */
transient private static final Logger LOGGER = Logger.getLogger(AlertRule.class);

/**
 * ID.
 */
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;

/**
 * Rule name.
 */
@NotNull
private String name;

...
Kari5
  • 188
  • 6

1 Answers1

7

With standard JPA, static fields are not persisted, and final fields are not persisted, so its not a problem

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • And what if i use cache: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)? In fact we have some memory leaks, and the heap dump showed this entity... – Kari5 Jun 25 '13 at 13:38
  • the class is initialised and it creates a Logger, that's all there is. The persistence process (whichever JPA impl you use) should do nothing with it (DataNucleus JPA certainly doesn't). – DataNucleus Jun 25 '13 at 13:40
  • 1
    If your logger is not static, use @Transient. see https://stackoverflow.com/questions/1281952/jpa-fastest-way-to-ignore-a-field-during-persistence – Nico Toub May 17 '18 at 09:02