I am writing a java spring application.
In my application there are two entity classes
... Counterparty:
@Entity
@Table(name="Counterparties")
public class Counterparty {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
//... getters and setters
}
... and Transaction:
@Entity
@Table(name="Transactions")
public class Transaction {
@Id
@Column(name="id")
private int id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="counterparty_id", referencedColumnName="id", nullable=false)
private Counterparty counterparty;
@Column(name="market_value", nullable=false)
private Double marketValue;
//... getters and setters
}
The database is supposed to be filled by reading xml-data into the database.
Here's a minimal xml-file with one counterparty and one transaction:
<?xml version="1.0" encoding="UTF-8"?>
<DataInput>
<Counterparty id="1" name="Deutsche Bank"/>
<Transaction id="1" counterpartyId="1" marketValue="200.00"/>
</DataInput>
I am writing a SAX-Parser to read the xml-data into the database-tables.
Here's the startElement-method of the SAXHandler:
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("Counterparty")) {
Counterparty counterparty = new Counterparty();
int id = Integer.parseInt(attributes.getValue("id"));
counterparty.setId(id);
String name = attributes.getValue("name");
counterparty.setName(name);
addCounterpartyService.saveCounterparty(counterparty);
}
else if (qName.equalsIgnoreCase("Transaction")) {
Transaction transaction = new Transaction();
int id = Integer.parseInt(attributes.getValue("id"));
inputTransaction.setId(id);
int counterpartyId = Integer.parseInt(attributes.getValue("counterpartyId"));
********************************************************************
//the following line of code throws an error:
transaction.setCounterparty(counterpartyId);
********************************************************************
Double marketValue = Double.valueOf(attributes.getValue("marketValue"));
transaction.setMarketValue(marketValue);
addTransactionService.saveTransaction(transaction);
}
}
I understand the reason for why this line throws an error: The method
transaction.setCounterparty()
expects a parameter of type "Counterparty" but gets one of type "int".
But what am I supposed to do? The xml-file contains integer values for the attribute counterpartyId.