0

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.

steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • 1
    Try to getCounterparty by id = 1 from database first (it has to be already persisted) and then set to Transaction. A better solution will be using diiffferent entities for xml and db. – rvit34 Oct 16 '17 at 22:54
  • 2
    @steady_progress probably can try https://stackoverflow.com/a/1608621/1032167 https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#getReference-java.lang.Class-java.lang.Object- https://stackoverflow.com/a/32036409/1032167 – varren Oct 16 '17 at 23:02
  • Thanks a lot! I did it as recommended by rvit34 – steady_progress Oct 17 '17 at 00:05

0 Answers0