Java:@ Overridden toString()不会打印任何内容

时间:2013-01-14 22:22:04

标签: java

我有一个@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();
    }
}

VariableManager创建@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不会打印对象的状态,即使我可以看到变量是在数据库中创建的。

1 个答案:

答案 0 :(得分:3)

从你的代码中,我不知道你正在使用什么类型的记录器(基于info()的方法签名,它似乎不是log4j也不是java.util记录器),但我怀疑调用

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

而不是

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

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