GetId () на двух разных идентификаторах - PullRequest
0 голосов
/ 01 июля 2019

Моя проблема в том, что я не могу вставить два разных идентификатора в свою базу данных.uhrid всегда совпадает с kundeid.

Я знаю, что проблема в entity.getId в моем классе дао.

public Bestellung insert(Bestellung entity) throws DbException {
            if (!this.hasConnection()) 
                throw new DbException("No connection to Database");

            try {
                // create sql command with placeholder
                String sql = "INSERT INTO " + this.tableName + " VALUES (NULL,?,?)";
                PreparedStatement preparedStatement = this.conn.getConnection().prepareStatement(sql);

                preparedStatement.setInt(1, entity.getId());
                preparedStatement.setInt(2, entity.getId());

                int affectedRows = preparedStatement.executeUpdate();

                if (affectedRows != 1) 
                    throw new SQLException("Insert failed");
                entity.setId(this.getLastAutoincrementId(preparedStatement));           
            } catch (SQLException e) {
                throw new DbException("Could not insert the entity=" + entity.toString(), e);
            }

            // return entity
            return entity;
        }
@POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createBestellung(
            @FormParam("uhrid") int uhrid,
            @FormParam("kundeid") int kundeid
            ) {
        System.out.println("BestellungService - createBestellung");

            Bestellung entity = new Bestellung();
            entity.setId(uhrid);
            entity.setId(kundeid);

            ResponseBuilder responseBuilder = null;
            DbConnection conn = null;
        try {

                conn = DbConnection.getInstance();
            BestellungDao dao = new BestellungDao(conn);

            entity = dao.insert(entity);

            System.out.println("Inserted entity: " + entity.toString());
            responseBuilder = Response.status(Status.OK).entity(entity);

        } catch (DbException | SQLException e) {

            e.printStackTrace();
            ServiceException se = new ServiceException("Fehler aufgetreten", e);
            responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR).entity(se.toJSON());
        } finally {
            conn.close();
        }

        return responseBuilder.build();
}}
public class Oberklasse {

    protected int id;

    public Oberklasse() {
        this.id = 0;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Oberklasse [id=" + id + "]";
    }
public class Kunde extends Oberklasse{

    private String anrede;
    private String vorname;
    private String nachname;
    private String adresse;

    ...

}

КлассUhr выглядит примерно так же.

Вывод в базу данных:

id | uhrid | kundeid 
---+-------+---------    
 1 |   3   |    3

Ожидаемый вывод, например:

id | uhrid | kundeid 
---+-------+---------    
 1 |   2   |   3

uhrid всегда совпадает с kundeid.

Я надеюсь, что кто-нибудь может помочь мне с моей проблемой.

Спасибо.

...