2

一直在玩弄 Hibernate 和 PostgreSQL,试图让它按预期工作。

但是由于某种原因,当我尝试将具有@OneToMany关系的对象与集合中的多个项目保持一致时,除了第一个项目之外,所有项目似乎都被忽略了。我已经通过本地和远程接口尝试过,但每次都得到相同的结果。没有抛出异常,看起来休眠在第一个单元被持久化后才停止持久化。

任何帮助都非常感谢这个人已经躲避了我好几天了。

我将 Hibernate 3.5 和 PostgreSQL 8.4.4 与 Glassfish v3 一起使用。源代码中的所有注释都来自包 javax.persistence - 由于字符限制,必须删除导入。

FacadeManager 是一个简单的实用程序,可以消除 jndi 查找的麻烦,它简化了访问远程外观。

这是测试方法

    @Test
    public void testOneToManyCascade() throws Exception {
        /*
         * Set up entities requred to perform the test
         */
        UnitFacadeRemote unitfr = FacadeManager
                .getFacade(UnitFacadeRemote.class);
        UserFacadeRemote userfr = FacadeManager
                .getFacade(UserFacadeRemote.class);

        User user = new User("P", "P", "000000", true, new Date(), "ypy@ypy",
                "wordof mout", "slacker", "password");

        Address address = new Address("yo", "go", "YOKO", "4123");
        address.setCountry(FacadeManager.getFacade(CountryFacadeRemote.class)
                .find(2));
        user.setAddress(address);

        Unit unit1 = new Unit(1, "Test Unit1", new Date(), "DisService",
                "MyNation");
        Unit unit2 = new Unit(2, "Test Unit2", new Date(), "DisService",
                "TheirNation");
        Unit unit3 = new Unit(3, "Test Unit3", new Date(), "DisService",
                "TheirNation");

        // Check no game exists
        assertThat(FacadeManager.getFacade(GameFacadeRemote.class).findAll()
                .size(), is(0));

        Game game = new Game("blabla", 3333, "A game!", true);

        // Create game and return reference to persisted game instance
        game = FacadeManager.getFacade(GameFacadeRemote.class).registerGame(
                game);

        unit1.setGame(game);
        unit2.setGame(game);
        unit3.setGame(game);

        // Find a virtue
        Virtue v = FacadeManager.getFacade(VirtueFacadeRemote.class).find(1);

        unit1.setVirtue(v);
        unit2.setVirtue(v);
        unit3.setVirtue(v);

        // check that none of the above exist already
        assertThat(userfr.findAll().size(), is(0));
        assertThat(unitfr.findAll().size(), is(0));

        /*
         * Try persisting the by cascading from the user
         */

        Set<Unit> unitSet = new HashSet<Unit>();
        unitSet.add(unit2);
        unitSet.add(unit1);
        unitSet.add(unit3);
        user.setUnitSet(unitSet);
        unit1.setUser(user);
        unit2.setUser(user);
        unit3.setUser(user);

        // Persist
        userfr.create(user);

        /*
         * The result of the preceding persist is that the game, user, address
         * and one unit are persisted, in this case unit2 but it seems whichever
         * unit is first added to unitSet is the one that is persisted.
         */
    }

Here follow the various entity classes
package com.game.database.entity;

@Entity
@Table(name = "address")
@NamedQueries({
        @NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a"),
        @NamedQuery(name = "Address.findById", query = "SELECT a FROM Address a WHERE a.id = :id"),
        @NamedQuery(name = "Address.findByLine1", query = "SELECT a FROM Address a WHERE a.line1 = :line1"),
        @NamedQuery(name = "Address.findByLine2", query = "SELECT a FROM Address a WHERE a.line2 = :line2"),
        @NamedQuery(name = "Address.findByCity", query = "SELECT a FROM Address a WHERE a.city = :city"),
        @NamedQuery(name = "Address.findByAreaCode", query = "SELECT a FROM Address a WHERE a.areaCode = :areaCode") })
public class Address implements Serializable {
    private static final long serialVersionUID = 1L;
    @SequenceGenerator(name = "address_id_seq", sequenceName = "address_id_seq", allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "address_id_seq")
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "line1")
    private String line1;
    @Basic(optional = false)
    @Column(name = "line2")
    private String line2;
    @Basic(optional = false)
    @Column(name = "city")
    private String city;
    @Basic(optional = false)
    @Column(name = "areaCode")
    private String areaCode;
    @JoinColumn(name = "country", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Country country;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "address")
    private Set<User> userSet;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "address")
    private Set<CardDetails> cardDetailsSet;

    public Address() {
    }

    public Address(Integer id) {
        this.id = id;
    }

    public Address(String line1, String line2, String city, String areaCode) {
        this.line1 = line1;
        this.line2 = line2;
        this.city = city;
        this.areaCode = areaCode;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLine1() {
        return line1;
    }

    public void setLine1(String line1) {
        this.line1 = line1;
    }

    public String getLine2() {
        return line2;
    }

    public void setLine2(String line2) {
        this.line2 = line2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public Set<User> getUserSet() {
        return userSet;
    }

    public void setUserSet(Set<User> userSet) {
        this.userSet = userSet;
    }

    public Set<CardDetails> getCardDetailsSet() {
        return cardDetailsSet;
    }

    public void setCardDetailsSet(Set<CardDetails> cardDetailsSet) {
        this.cardDetailsSet = cardDetailsSet;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof Address)) {
            return false;
        }
        Address other = (Address) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.game.database.entity.Address[id=" + id + "]";
    }
}

--

package com.game.database.entity;

@Entity
@Table(name = "game")
@NamedQueries({
        @NamedQuery(name = "Game.findAll", query = "SELECT g FROM Game g"),
        @NamedQuery(name = "Game.findById", query = "SELECT g FROM Game g WHERE g.id = :id"),
        @NamedQuery(name = "Game.findByHost", query = "SELECT g FROM Game g WHERE g.host = :host"),
        @NamedQuery(name = "Game.findByPort", query = "SELECT g FROM Game g WHERE g.port = :port"),
        @NamedQuery(name = "Game.findByDescription", query = "SELECT g FROM Game g WHERE g.description = :description"),
        @NamedQuery(name = "Game.findByActive", query = "SELECT g FROM Game g WHERE g.active = :active"),
        @NamedQuery(name = "Game.findByHostAndPort", query = "SELECT g FROM Game g WHERE g.host = :host AND g.port = :port") })
public class Game implements Serializable {
    private static final long serialVersionUID = 1L;
    @SequenceGenerator(name = "game_id_seq", sequenceName = "game_id_seq", allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "game_id_seq")
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "host")
    private String host;
    @Basic(optional = false)
    @Column(name = "port")
    private int port;
    @Basic(optional = false)
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @Column(name = "active")
    private Boolean active;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "game")
    private Set<Unit> unitSet;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "game")
    private Set<Payment> paymentSet;

    public Game() {
    }

    public Game(Integer id) {
        this.id = id;
    }

    public Game(String host, int port, String description, Boolean active) {
        this.host = host;
        this.port = port;
        this.description = description;
        this.active = active;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public Set<Unit> getUnitSet() {
        return unitSet;
    }

    public void setUnitSet(Set<Unit> unitSet) {
        this.unitSet = unitSet;
    }

    public Set<Payment> getPaymentSet() {
        return paymentSet;
    }

    public void setPaymentSet(Set<Payment> paymentSet) {
        this.paymentSet = paymentSet;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof Game)) {
            return false;
        }
        Game other = (Game) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.game.database.entity.Game[id=" + id + "]";
    }
}

--

package com.game.database.entity;

@Entity
@Table(name = "unit")
@NamedQueries({
        @NamedQuery(name = "Unit.findAll", query = "SELECT u FROM Unit u"),
        @NamedQuery(name = "Unit.findById", query = "SELECT u FROM Unit u WHERE u.id = :id"),
        @NamedQuery(name = "Unit.findByUnitId", query = "SELECT u FROM Unit u WHERE u.unitId = :unitId"),
        @NamedQuery(name = "Unit.findByName", query = "SELECT u FROM Unit u WHERE u.name = :name"),
        @NamedQuery(name = "Unit.findByRegistrationDate", query = "SELECT u FROM Unit u WHERE u.registrationDate = :registrationDate"),
        @NamedQuery(name = "Unit.findByService", query = "SELECT u FROM Unit u WHERE u.service = :service"),
        @NamedQuery(name = "Unit.findByNation", query = "SELECT u FROM Unit u WHERE u.nation = :nation") })
public class Unit implements Serializable {
    private static final long serialVersionUID = 1L;
    @SequenceGenerator(name = "unit_id_seq", sequenceName = "unit_id_seq", allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "unit_id_seq")
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "unitId")
    private Integer unitId;
    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @Column(name = "registrationDate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date registrationDate;
    @Basic(optional = false)
    @Column(name = "service")
    private String service;
    @Basic(optional = false)
    @Column(name = "nation")
    private String nation;
    @JoinColumn(name = "virtue", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Virtue virtue;
    @JoinColumn(name = "customer", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User customer;
    @JoinColumn(name = "game", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Game game;

    public Unit() {
    }

    public Unit(Integer id) {
        this.id = id;
    }

    public Unit(Integer unitId, String name, Date registrationDate,
            String service, String nation) {
        this.unitId = unitId;
        this.name = name;
        this.registrationDate = registrationDate;
        this.service = service;
        this.nation = nation;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUnitId() {
        return unitId;
    }

    public void setUnitId(Integer unitId) {
        this.unitId = unitId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public Virtue getVirtue() {
        return virtue;
    }

    public void setVirtue(Virtue virtue) {
        this.virtue = virtue;
    }

    public User getUser() {
        return customer;
    }

    public void setUser(User user) {
        this.customer = user;
    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        Logger.getLogger("org.hibernate").setLevel(Level.FINEST);
        this.game = game;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof Unit)) {
            return false;
        }
        Unit other = (Unit) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.game.database.entity.Unit[id=" + id + "]";
    }

    @Transient
    private String command;

    /**
     * Get the value of command
     * 
     * @return the value of command
     */
    public String getCommand() {
        return command;
    }

    /**
     * Set the value of command
     * 
     * @param command
     *            new value of command
     */
    public void setCommand(String command) {
        this.command = command;
    }

    @Transient
    private String rank;

    /**
     * Get the value of rank
     * 
     * @return the value of rank
     */
    public String getRank() {
        return rank;
    }

    /**
     * Set the value of rank
     * 
     * @param rank
     *            new value of rank
     */
    public void setRank(String rank) {
        this.rank = rank;
    }

    @Transient
    private BigDecimal price;

    /**
     * Get the value of price
     * 
     * @return the value of price
     */
    public BigDecimal getPrice() {
        return price;
    }

    /**
     * Set the value of price
     * 
     * @param price
     *            new value of price
     */
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
}

--

package com.game.database.entity;

@Entity
@Table(name = "customer")
@NamedQueries({
        @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
        @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
        @NamedQuery(name = "User.findByGivenName", query = "SELECT u FROM User u WHERE u.givenName = :givenName"),
        @NamedQuery(name = "User.findBySurname", query = "SELECT u FROM User u WHERE u.surname = :surname"),
        @NamedQuery(name = "User.findByPhoneNumber", query = "SELECT u FROM User u WHERE u.phoneNumber = :phoneNumber"),
        @NamedQuery(name = "User.findBySex", query = "SELECT u FROM User u WHERE u.sex = :sex"),
        @NamedQuery(name = "User.findByBirthYear", query = "SELECT u FROM User u WHERE u.birthYear = :birthYear"),
        @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
        @NamedQuery(name = "User.findByInfoSource", query = "SELECT u FROM User u WHERE u.infoSource = :infoSource"),
        @NamedQuery(name = "User.findByOccupation", query = "SELECT u FROM User u WHERE u.occupation = :occupation"),
        @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
        @NamedQuery(name = "User.findByEmailAndPassword", query = "SELECT u FROM User u WHERE u.password = :password AND u.email = :email") })
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @SequenceGenerator(name = "user_id_seq", sequenceName = "user_id_seq", allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq")
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "givenName")
    private String givenName;
    @Basic(optional = false)
    @Column(name = "surname")
    private String surname;
    @Basic(optional = false)
    @Column(name = "phoneNumber")
    private String phoneNumber;
    @Basic(optional = false)
    @Column(name = "sex")
    private boolean sex;
    @Basic(optional = false)
    @Column(name = "birthYear")
    @Temporal(TemporalType.DATE)
    private Date birthYear;
    @Basic(optional = false)
    @Column(name = "email")
    private String email;
    @Basic(optional = false)
    @Column(name = "infoSource")
    private String infoSource;
    @Basic(optional = false)
    @Column(name = "occupation")
    private String occupation;
    @Basic(optional = false)
    @Column(name = "password")
    private String password;
    /**
     * The EAGER fetch type ensures that when we get access a unit remotely it
     * has had its unit set initialised and populated
     */
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", fetch = FetchType.EAGER)
    private Set<Unit> unitSet;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", fetch = FetchType.EAGER)
    private Set<Payment> paymentSet;
    @JoinColumn(name = "address", referencedColumnName = "id")
    @ManyToOne(cascade = CascadeType.ALL, optional = false)
    private Address address;

    public User() {
    }

    public User(Integer id) {
        this.id = id;
    }

    public User(String givenName, String surname, String phoneNumber,
            boolean sex, Date birthYear, String email, String infoSource,
            String occupation, String password) {
        this.givenName = givenName;
        this.surname = surname;
        this.phoneNumber = phoneNumber;
        this.sex = sex;
        this.birthYear = birthYear;
        this.email = email;
        this.infoSource = infoSource;
        this.occupation = occupation;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public boolean getSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public Date getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(Date birthYear) {
        this.birthYear = birthYear;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getInfoSource() {
        return infoSource;
    }

    public void setInfoSource(String infoSource) {
        this.infoSource = infoSource;
    }

    public String getOccupation() {
        return occupation;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<Unit> getUnitSet() {
        return unitSet;
    }

    public void setUnitSet(Set<Unit> unitSet) {
        this.unitSet = unitSet;
    }

    public Set<Payment> getPaymentSet() {
        return paymentSet;
    }

    public void setPaymentSet(Set<Payment> paymentSet) {
        this.paymentSet = paymentSet;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.game.database.entity.User[id=" + id + "]";
    }
}

--

package com.game.database.entity;

@Entity
@Table(name = "virtue")
@NamedQueries({
        @NamedQuery(name = "Virtue.findAll", query = "SELECT v FROM Virtue v"),
        @NamedQuery(name = "Virtue.findById", query = "SELECT v FROM Virtue v WHERE v.id = :id"),
        @NamedQuery(name = "Virtue.findByName", query = "SELECT v FROM Virtue v WHERE v.name = :name"),
        @NamedQuery(name = "Virtue.findByDescription", query = "SELECT v FROM Virtue v WHERE v.description = :description") })
public class Virtue implements Serializable {
    private static final long serialVersionUID = 1L;
    @SequenceGenerator(name = "virtue_id_seq", sequenceName = "virtue_id_seq", allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "virtue_id_seq")
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    @Basic(optional = false)
    @Column(name = "description")
    private String description;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "virtue")
    private Set<Unit> unitSet;

    public Virtue() {
    }

    public Virtue(Integer id) {
        this.id = id;
    }

    public Virtue(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<Unit> getUnitSet() {
        return unitSet;
    }

    public void setUnitSet(Set<Unit> unitSet) {
        this.unitSet = unitSet;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof Virtue)) {
            return false;
        }
        Virtue other = (Virtue) object;
        if ((this.id == null && other.id != null)
                || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.game.database.entity.Virtue[id=" + id + "]";
    }
}
4

1 回答 1

3

您对 Unit 对象的标识有疑问。您在代码中使用了 HashSet,它依赖于在您的代码中被覆盖的 equals 和 hashCode 方法。

如果您在下面的代码中运行 main 方法,您将看到添加 Unit 对象后,集合中只有一个(我重用了您的原始代码,但我对其进行了一些简化):

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Unit {

    private Integer id;

    private Integer unitId;
    private String name;
    private Date registrationDate;

    private String service;

    private String nation;

    public Unit() {
    }

    public Unit(Integer id) {
        this.id = id;
    }

    public Unit(Integer unitId, String name, Date registrationDate, String service, String nation) {
        this.unitId = unitId;
        this.name = name;
        this.registrationDate = registrationDate;
        this.service = service;
        this.nation = nation;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUnitId() {
        return unitId;
    }

    public void setUnitId(Integer unitId) {
        this.unitId = unitId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getRegistrationDate() {
        return registrationDate;
    }

    public void setRegistrationDate(Date registrationDate) {
        this.registrationDate = registrationDate;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }


    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Unit)) {
            return false;
        }
        Unit other = (Unit) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.game.database.entity.Unit[id=" + id + "]";
    }


    public static void main(String... args) {

        Unit unit1 = new Unit(1, "Test Unit1", new Date(), "DisService", "MyNation");
        Unit unit2 = new Unit(2, "Test Unit2", new Date(), "DisService", "TheirNation");
        Unit unit3 = new Unit(3, "Test Unit3", new Date(), "DisService", "TheirNation");

        Set unitSet = new HashSet();
        unitSet.add(unit2);
        unitSet.add(unit1);
        unitSet.add(unit3);

        System.out.println(unitSet.size());

    }

如果您注释掉 equals() 和 hashCode() 并再次运行代码,您将在集合中看到三个对象。

您的 equals() 方法基于 hibernate 在持久化对象时而不是在添加到集合时设置的 id 属性。这意味着 Unit 在添加到集合时始终具有 null id 和 hashCode = 0。我推荐阅读这篇文章:http: //community.jboss.org/wiki/EqualsandHashCode

希望能帮助到你!

于 2010-09-01T12:07:59.653 回答