0

我有@Entity一个

@Entity
@Table(name = "variable")
@XmlRootElement
public class Variable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true, nullable = false)
    private String name;

    @Column
    @Enumerated(EnumType.STRING)
    private VariableType type;

    @Column(nullable = false)
    private String units;

    @Column
    private String description;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_on", nullable = false)
    private Date createdOn;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "retired_on", nullable = true)
    private Date retiredOn;

    @Column(nullable = false)
    private boolean core;

    public Variable(@Nonnull final String name,
                    @Nonnull final VariableType type,
                    @Nonnull final String units,
                    @Nonnull final String description,
                    @Nonnull final Date createdOn,
                    @Nonnull final boolean core) {
        this.name = name;
        this.type = type;
        this.units = units;
        this.description = description;
        this.core = core;
        this.createdOn = createdOn;
    }

    @Nonnull
    public long getId() {
        return id;
    }

    @Nonnull
    public String getName() {
        return name;
    }

    @Nonnull
    public VariableType getType() {
        return type;
    }

    @Nonnull
    public String getUnits() {
        return units;
    }

    @Nullable
    public String getDescription() {
        return description;
    }

    @Nonnull
    public Date getCreatedOn() {
        return createdOn;
    }

    @Nonnull
    public Date getRetiredOn() {
        return retiredOn;
    }

    @Nonnull
    public boolean isCore() {
        return core;
    }

    @Override
    public String toString() {
        return new StringBuilder().
                append("id="+id+" ").
                append("name="+name+" ").
                append("type="+type+" ").
                append("units="+units+" ").
                append("description="+description+" ").
                append("createdOn="+createdOn+" ").
                append("retiredOn="+retiredOn+" ").
                append("core="+core+" ").
                toString();
    }
}

和 aVariableManager创建@Entity实例

public Variable createVariable(@Nonnull final String name,
                                   @Nonnull final VariableType type,
                                   @Nonnull final String units,
                                   @Nonnull final String description,
                                   @Nonnull final boolean core) {
        final Variable variable =  variableCrudService.create(new Variable(name, type, units, description, new Date(), core));
        LOGGER.info("added variable - ", variable);
        return variable;
    }

当我执行此操作时,我得到的日志信息为

14:18:12,320 INFO  [org.jboss.resteasy.cdi.CdiInjectorFactory] (http--127.0.0.1-8080-4) Found BeanManager at java:comp/BeanManager
14:18:13,336 INFO  [com.bb.business.VariableManager] (http--127.0.0.1-8080-4) added variable - 

我不知道为什么 toString 不打印对象的状态,即使我可以看到变量是在数据库中创建的。

4

1 回答 1

3

从您的代码中,我无法判断您使用的是哪种类型的记录器(基于 info() 的方法签名,它似乎不是 log4j 也不是 java.util 记录器),但我怀疑调用

LOGGER.info("added variable - " + variable);

代替

LOGGER.info("added variable - ", variable);

(注意加号而不是逗号)可以做到。

于 2013-01-14T22:30:49.417 回答