Here's my console test app.
I commented out every line that uses Message class because Player class already throws exception, but Message class will be usefull after Player class will work.
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
@Entity
class Player implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String playerName;
@ManyToMany
private List<Player> friendWishList = new ArrayList<Player>();
@ManyToMany(mappedBy = "friendWishList")
private List<Player> possibleFriends;
// @ManyToOne
// private List<Message> postedMessages = new ArrayList<Message>();
//
// @OneToMany(mappedBy = "address")
// private List<Message> receivedMessages;
protected Player() {
}
public Player(String playerName) {
this.playerName = playerName;
}
public Long getId() {
return id;
}
public String getPlayerName() {
return playerName;
}
// public List<Message> getReceivedMessages() {
// return receivedMessages;
// }
//
// public List<Message> getPostedMessages() {
// return postedMessages;
// }
public List<Player> getFriendWishList() {
return friendWishList;
}
public List<Player> getPossibleFriends() {
return possibleFriends;
}
@Override
public String toString() {
return playerName + '#' + getId();
}
}
//@Entity
//class Message implements Serializable {
//
// private static final long serialVersionUID = 1L;
//
// @Id
// @GeneratedValue
// private Long id;
//
// @Temporal(TemporalType.TIMESTAMP)
// private Date sendDate = new Date();
//
// private String text;
//
// @OneToMany(mappedBy="postedMessages")
// private Player sender;
//
// @ManyToOne
// private Player address;
//
// protected Message() {
// }
//
// public Message(Player address, String text) {
// this.address = address;
// this.text = text;
// }
//
// public Long getId() {
// return id;
// }
//
// public String getText() {
// return text;
// }
//
// public Date getSendDate() {
// return sendDate;
// }
//
// public Player getSender() {
// return sender;
// }
//
// public Player getAddress() {
// return address;
// }
//
// public void setText(String text) {
// this.text = text;
// }
//
// @Override
// public String toString() {
// return "Message#" + getId();
// }
//
//}
public class Main {
private static final String PLAYER1 = "player1", PLAYER2 = "player2";
private static EntityManager db;
private static EntityManager createEntityManager() {
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.user", "admin");
properties.put("javax.persistence.jdbc.password", "admin");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("objectdb://localhost:6136/fzoli_bugreport.odb", properties);
return emf.createEntityManager();
}
private static boolean isDatabaseEmpty() {
try {
return db.createQuery("SELECT count(p) = 0 FROM Player p", Boolean.class).getSingleResult();
}
catch (PersistenceException ex) {
ex.printStackTrace();
return false;
}
}
private static Player getPlayer(String name) {
if (name == null) return null;
try {
TypedQuery<Player> query = db.createQuery("SELECT p FROM Player p WHERE upper(p.playerName) = upper(:name)", Player.class);
return query.setParameter("name", name).getSingleResult();
}
catch (PersistenceException ex) {
ex.printStackTrace();
return null;
}
}
// private static List<Message> getMessages(String playerName) {
// if (playerName == null) return null;
// try {
// TypedQuery<Message> query = db.createQuery("SELECT m FROM Message m WHERE m.sender.playerName = :name", Message.class);
// return query.setParameter("name", playerName).getResultList();
// }
// catch (PersistenceException ex) {
// ex.printStackTrace();
// return null;
// }
// }
private static boolean save(Object obj) {
EntityTransaction tr = db.getTransaction();
try {
tr.begin();
db.persist(obj);
tr.commit();
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
db = createEntityManager(); // create database connection
if (isDatabaseEmpty()) { // database is empty, create entity classes
System.out.println("Initialize test database.");
// create two player
Player p1 = new Player(PLAYER1);
save(p1);
Player p2 = new Player(PLAYER2);
save(p2);
// add player2 to player1's friend wish list
p1.getFriendWishList().add(p2);
save(p1);
// add player1 to player2's friend wish list
// IF YOU DON'T ADD, NO EXCEPTION SHOULD BE THROWN
p2.getFriendWishList().add(p1);
save(p2);
// /* create new message to player1 from player2 */
// Message m = new Message(p1, "test message");
// p2.getPostedMessages().add(m);
// save(m);
// save(p2);
//
// /* create new message to player2 from player1 */
// m = new Message(p2, "test message 2");
// p1.getPostedMessages().add(m);
// save(m);
// save(p1);
// create new database connection in order to cause exception
db.close();
db = createEntityManager();
}
else {
System.out.println("Database already initialized.");
}
System.out.println("Get " + PLAYER1 + " from database.");
System.out.println(getPlayer(PLAYER1)); // getPlayer() should cause exception
// System.out.println("Get " + PLAYER1 + "'s messages.");
// System.out.println(getMessages(PLAYER1)); // getMessages() should cause exception too
db.close();
}
}
And the result:
Database already initialized.
Get player1 from database.
[ObjectDB 2.4.1_07] javax.persistence.PersistenceException
Failed to write the value of field field testcase.Player.possibleFriends using reflection (error 363)
at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:723)
at testcase.Main.getPlayer(Main.java:155)
at testcase.Main.main(Main.java:230)
Caused by: com.objectdb.o.UserException: Failed to write the value of field field testcase.Player.possibleFriends using reflection
at com.objectdb.o.MSG.d(MSG.java:74)
at com.objectdb.o.UMR.M(UMR.java:907)
at com.objectdb.o.UMR.x(UMR.java:567)
at com.objectdb.o.UML.u(UML.java:536)
at com.objectdb.o.ENH.d(ENH.java:234)
at com.objectdb.o.ENT.T(ENT.java:851)
at com.objectdb.o.LDR.y(LDR.java:484)
at com.objectdb.o.LDR.x(LDR.java:448)
at com.objectdb.o.LDR.s(LDR.java:161)
at com.objectdb.o.OBC.aN(OBC.java:1076)
at com.objectdb.o.OBC.aL(OBC.java:976)
at com.objectdb.o.OBC.UG(OBC.java:815)
at com.objectdb.o.SRB.l(SRB.java:149)
at com.objectdb.o.QRR.b(QRR.java:187)
at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:716)
... 2 more
Caused by: com.objectdb.o._PersistenceException: Failed to write the value of field field testcase.Player.possibleFriends using reflection
at com.objectdb.o._PersistenceException.b(_PersistenceException.java:45)
at com.objectdb.o.JPE.g(JPE.java:142)
at com.objectdb.o.ERR.f(ERR.java:60)
at com.objectdb.o.IVP.h(IVP.java:107)
at com.objectdb.o.IVP.g(IVP.java:87)
at com.objectdb.o.ILP.isEmpty(ILP.java:136)
at com.objectdb.o.TYW.av(TYW.java:477)
at com.objectdb.o.TYW.writeElement(TYW.java:305)
at com.objectdb.o.UMR$P.y(UMR.java:975)
at com.objectdb.o.UMR.x(UMR.java:564)
... 14 more
Caused by: com.objectdb.o.UserException: Failed to write the value of field field testcase.Player.possibleFriends using reflection
at com.objectdb.o.MSG.d(MSG.java:74)
at com.objectdb.o.UMR.M(UMR.java:907)
at com.objectdb.o.UMR.x(UMR.java:567)
at com.objectdb.o.UML.u(UML.java:536)
at com.objectdb.o.ENH.d(ENH.java:234)
at com.objectdb.o.ENT.T(ENT.java:851)
at com.objectdb.o.LDR.y(LDR.java:484)
at com.objectdb.o.LDR.x(LDR.java:448)
at com.objectdb.o.LDR.s(LDR.java:161)
at com.objectdb.o.OBC.aN(OBC.java:1076)
at com.objectdb.o.OBC.aL(OBC.java:976)
at com.objectdb.o.OBC.UG(OBC.java:815)
at com.objectdb.o.SRB.l(SRB.java:149)
at com.objectdb.o.QRR.m(QRR.java:544)
at com.objectdb.o.QRR.b(QRR.java:206)
at com.objectdb.o.QRR.b(QRR.java:651)
at com.objectdb.o.UMR.u(UMR.java:516)
at com.objectdb.o.ENT.loadInverse(ENT.java:1394)
at com.objectdb.o.IVP.h(IVP.java:100)
... 20 more
Caused by: com.objectdb.o._PersistenceException: Failed to write the value of field field testcase.Player.possibleFriends using reflection
at com.objectdb.o._PersistenceException.b(_PersistenceException.java:45)
at com.objectdb.o.JPE.g(JPE.java:142)
at com.objectdb.o.ERR.f(ERR.java:60)
at com.objectdb.o.IVP.h(IVP.java:107)
at com.objectdb.o.IVP.g(IVP.java:87)
at com.objectdb.o.ILP.isEmpty(ILP.java:136)
at com.objectdb.o.TYW.av(TYW.java:477)
at com.objectdb.o.TYW.writeElement(TYW.java:305)
at com.objectdb.o.UMR$P.y(UMR.java:975)
at com.objectdb.o.UMR.x(UMR.java:564)
... 36 more
Caused by: com.objectdb.o.UserException: Failed to write the value of field field testcase.Player.possibleFriends using reflection
at com.objectdb.o.MSG.d(MSG.java:74)
at com.objectdb.o.UMR.M(UMR.java:907)
at com.objectdb.o.UMR.x(UMR.java:567)
at com.objectdb.o.UML.u(UML.java:536)
at com.objectdb.o.ENH.d(ENH.java:234)
at com.objectdb.o.ENT.T(ENT.java:851)
at com.objectdb.o.LDR.y(LDR.java:484)
at com.objectdb.o.LDR.x(LDR.java:448)
at com.objectdb.o.LDR.s(LDR.java:161)
at com.objectdb.o.OBC.aN(OBC.java:1076)
at com.objectdb.o.OBC.aM(OBC.java:1019)
at com.objectdb.o.OBC.aL(OBC.java:924)
at com.objectdb.o.OBC.UG(OBC.java:815)
at com.objectdb.o.SRB.l(SRB.java:149)
at com.objectdb.o.QRR.m(QRR.java:544)
at com.objectdb.o.QRR.b(QRR.java:206)
at com.objectdb.o.QRR.b(QRR.java:651)
at com.objectdb.o.UMR.u(UMR.java:516)
at com.objectdb.o.ENT.loadInverse(ENT.java:1394)
at com.objectdb.o.IVP.h(IVP.java:100)
... 42 more
Caused by: com.objectdb.o._PersistenceException: Failed to write the value of field field testcase.Player.possibleFriends using reflection
[...]