How to fix “java.sql.SQLException: Column 'id' not found.” error in Spring Boot

ⅰ亾dé卋堺 提交于 2021-02-05 11:34:09

问题


There are 2 entities: Deck and Card. Each Deck contains 8 out of 100 possible Cards. Since each Deck contains more than one Card and the Card can also be part of many different Decks, I joined them by creating a new table called deck_cards.

@Data
@Entity
@Table(name = "cards")
public class Card {
    @Id
    private Integer id;

    @NotBlank
    private String icon;

    @NotBlank
    private String name;

    @ManyToMany(fetch = FetchType.LAZY,
                cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
                },
                mappedBy = "cards")
    private Set<Deck> decks;
// setters and getters
}

@Data
@Entity
@Table(name="decks")
public class Deck {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToMany(fetch = FetchType.LAZY,
                cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
                })
    @JoinTable(name = "deck_cards",
            joinColumns = { @JoinColumn(name = "deck_id", 
referencedColumnName = "id")},
            inverseJoinColumns = { @JoinColumn(name = "card_id", 
referencedColumnName = "id")})
    private Set<Card> cards = new HashSet<>();
// setters and getters
}

@Repository
@Transactional(readOnly = true)
public class DeckRepositoryImpl implements DeckRepositoryCustom {

    @PersistenceContext
    EntityManager entityManager;

    @Override
    public Deck getDeckContaining(List<String> cardIds) {
        String queryStr =
                "select deck_id\n" +
                "from deck_cards\n" +
                // Hard-coding card_ids of interest for now to 
                // see if the query itself works
                "where card_id in (1, 2, 3, 4, 5, 6, 7, 8)\n" +
                "group by deck_id\n" +
                "having count(*) = 8";
        Query query = entityManager.createNativeQuery(queryStr, 
Deck.class);
    return (Deck) query.getSingleResult();
}

I have a custom-defined method named getDeckContaining(), which was originally designed to take in List cardIds and see if there's a deck containing the ids stored in the list. For now, I hard-coded the ids of cards to look for in the query string. Even then, when I invoke the method like this:

    List<String> teamCardIdsInString = new ArrayList<>();
    //....
    Deck deckOfInterest = 
    deckRepository.getDeckContaining(teamCardIdsInString);

I am getting the following error message:

2019-07-29 19:00:06.663  WARN 3409 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S0022
2019-07-29 19:00:06.664 ERROR 3409 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'id' not found.
2019-07-29 19:00:06.690 ERROR 3409 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select deck_id
from deck_cards
where card_id in (26000060, 28000017, 26000044, 26000039, 26000027, 28000015, 26000033, 28000000)
group by deck_id
having count(*) = 8]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause

java.sql.SQLException: Column 'id' not found.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.16.jar:8.0.16]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.16.jar:8.0.16]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.16.jar:8.0.16]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.16.jar:8.0.16]
        at com.mysql.cj.jdbc.result.ResultSetImpl.findColumn(ResultSetImpl.java:561) ~[mysql-connector-java-8.0.16.jar:8.0.16]
        at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:825) ~[mysql-connector-java-8.0.16.jar:8.0.16]
        at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java) ~[HikariCP-3.2.0.jar:na]
        at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:62) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
        at 

I am confused since I thought I used the field names as defined in the entities. Is there something that I might have missed?


回答1:


java.sql.SQLException: Column 'id' not found. The issue is with the name of the column. referencedColumnName = "id" is the possible suspect here (below)

Possible Solution:

  • Remove referencedColumnName = "id"
  • As you haven't posted name as per your DB scheme (and also there is no @Column) make sure that id field in your DB exist


来源:https://stackoverflow.com/questions/57263494/how-to-fix-java-sql-sqlexception-column-id-not-found-error-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!