This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Hi, I'm trying to write out a class to XML using XMLEncoder.
There's a few unique things about this class.
1) It has no default constructor 2) I'm overriding the equals method. 3) The class has a static int count which defaults to 0. Each object is assigned an ID based on the current count. The ID is used to compare two objects.
Since it has no default constructor, I'm using the defaultpersistencedelegate. Here's the code:
If you run this, you will get a stackoverflowerror (atleast under jdk1.6.0_01 on Windows). I'm not an XMLEncoder expert, I've narrowed the problem down and I came up with this simple Person.java example.
The combination of the static int ID and override equals seems to be the problem. If you change the equals to:
It will still give the same error. However, if you remove the equals method completely, it will work fine and the xml output is as I expected. But unfortunately, I need to override the equals method.
1) Does anyone know how I can fix this? 2) Why would "return super.equals(obj)" give different behavior than having no equals method at all?
Note: I realize if my class were really a Person class, the equals method should compare SSN, first and last name, etc. and not some made up int ID - this is just a simple example to demonstrate the problem.
Joined: Mar 17, 2007
I solved it using my own persistence delegate to create a new Person. I don't understand why I had to do this, since I thought that was the purpose of the defaultpersistencedelegate. I did see some stuff in the source for defaultpersistencedelegate that involved checks for overriding equals - but I won't pretend like I understand how it works.