Java 坚持一个一个关系只坚持集合中的第一个对象?
一直在摆弄Hibernate和PostgreSQL,试图让它按预期工作 但出于某种原因,当我试图持久化一个对象,该对象具有@OneToMany关系,并且集合中有多个项时,除了第一项之外,所有项似乎都被忽略。我已经通过本地和远程接口尝试过了,但每次都得到了相同的结果。没有抛出异常,看起来hibernate只是在第一个单元被持久化后停止持久化 我已经好几天没有得到任何帮助了 我在Glassfish v3中使用Hibernate 3.5和PostgreSQL 8.4.4。源代码中的所有注释都来自javax.persistence包——由于字符限制,必须删除导入 FacadeManager只是一个实用程序,它简化了对远程Facade的访问,从而消除了jndi查找的麻烦 这是测试方法Java 坚持一个一个关系只坚持集合中的第一个对象?,java,hibernate,jpa,cascade,one-to-many,Java,Hibernate,Jpa,Cascade,One To Many,一直在摆弄Hibernate和PostgreSQL,试图让它按预期工作 但出于某种原因,当我试图持久化一个对象,该对象具有@OneToMany关系,并且集合中有多个项时,除了第一项之外,所有项似乎都被忽略。我已经通过本地和远程接口尝试过了,但每次都得到了相同的结果。没有抛出异常,看起来hibernate只是在第一个单元被持久化后停止持久化 我已经好几天没有得到任何帮助了 我在Glassfish v3中使用Hibernate 3.5和PostgreSQL 8.4.4。源代码中的所有注释都来自jav
@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;
@实体
@表(name=“客户”)
@命名查询({
@NamedQuery(name=“User.findAll”,query=“从用户u中选择u”),
@NamedQuery(name=“User.findById”,query=“从用户u中选择u,其中u.id=:id”),
@NamedQuery(name=“User.findByGivenName”,query=“从用户u中选择u,其中u.givenName=:givenName”),
@NamedQuery(name=“User.findBySurname”,query=“从用户u中选择u,其中u.姓氏=:姓氏”),
@NamedQuery(name=“User.findByPhoneNumber”,query=“从用户u中选择u,其中u.phoneNumber=:phoneNumber”),
@NamedQuery(name=“User.findBySex”,query=“从用户u中选择u,其中u.sex=:sex”),
@NamedQuery(name=“User.findByBirthYear”,query=“从用户u中选择u,其中u.birthYear=:birthYear”),
@NamedQuery(name=“User.findByEmail”,query=“从用户u中选择u,其中u.email=:email”),
@NamedQuery(name=“User.findByInfoSource”,query=“从用户u中选择u,其中u.infoSource=:infoSource”),
@NamedQuery(name=“User.findByOccupation”,query=“从用户u中选择u,其中u.occulation=:occulation”),
@NamedQuery(name=“User.findByPassword”,query=“从用户u中选择u,其中u.password=:password”),
@NamedQuery(name=“User.findByEmailAndPassword”,query=“从用户u中选择u,其中u.password=:password和u.email=:email”)}
公共类用户实现可序列化{
私有静态最终长serialVersionUID=1L;
@SequenceGenerator(name=“user\u id\u seq”,sequenceName=“user\u id\u seq”,allocationSize=1)
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE,generator=“用户id_seq”)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@列(name=“givenName”)
私人字符串givenName;
@基本(可选=假)
@列(name=“姓氏”)
私家姓;
@基本(可选=假)
@列(name=“phoneNumber”)
私有字符串电话号码;
@基本(可选=假)
@列(name=“sex”)
私人布尔性别;
@基本(可选=假)
@列(name=“生日”)
@时态(TemporalType.DATE)
私人生日;
@基本(可选=假)
@列(name=“email”)
私人字符串电子邮件;
@基本(可选=假)
@列(name=“infoSource”)
私有字符串信息源;
@基本(可选=假)
@列(名称=“职业”)
私人字符串占用;
@基本(可选=假)
@列(name=“password”)
私有字符串密码;
/**
*急切获取类型确保当我们远程访问一个单元时
*已初始化并填充其单元集
*/
@OneToMany(cascade=CascadeType.ALL,mappedBy=“customer”,fetch=FetchType.EAGER)
私有集单位集;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“customer”,fetch=FetchType.EAGER)
私人设置工资设置;
@JoinColumn(name=“address”,referencedColumnName=“id”)
@多通(cascade=CascadeType.ALL,可选=false)
私人地址;
公共用户(){
}
公共用户(整数id){
this.id=id;
}
公共用户(字符串givenName、字符串姓氏、字符串phoneNumber、,
布尔性别、生日、字符串电子邮件、字符串信息源、,
字符串占用,字符串密码){
this.givenName=givenName;
this.姓氏=姓氏;
this.phoneNumber=电话号码;
这个。性=性;
this.birthYear=生日;
this.email=电子邮件;
this.infoSource=infoSource;
这个职业=职业;
this.password=密码;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getGivenName(){
回报给我;
}
public void setGivenName(字符串givenName){
this.givenName=givenName;
}
公共字符串getNames(){
返回姓氏;
}
public void setSurname(字符串姓氏){
this.姓氏=姓氏;
}
公共字符串getPhoneNumber(){
返回电话号码;
}
public void setPhoneNumber(字符串phoneNumber){
this.phoneNumber=电话号码;
}
公共布尔getSex(){
回归性;
}
公共void setex(布尔性别){
这个。性=性;
}
公开日期:出生年月日{
返回出生年份;
}
公众出生年份(出生日期){
this.birthYear=生日;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getInfoSource(){
返回信息源;
}
public void setInfoSource(字符串infoSource){
this.infoSource=infoSource;
}
公共字符串getOccupation(){
返回职业;
}
公共无效设置占用(字符串占用){
这
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 + "]";
}
}
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());
}