Search code examples
javaspring-dataprimary-keyspring-data-jdbc

Am I missing something if I use my entity class without @Id in Spring Data JDBC?


I am new to spring.

I just tried successfully using an entity class without @Id in Spring Data JDBC

Custom query was added in my repository for retrieving data from 2 mysql tables and returning an entity having the joined table data.

If I plan to use only custom queries, am I missing anything here?

Here's my entity class without @Id or @Entity:

public class Item 
{
private long id;
private String code;
private String itemName;
private String groupName;

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getCode() {
    return code;
}
public void setCode(String code) {
    this.code = code;
}
public String getItemName() {
    return itemName;
}
public void setItemName(String itemName) {
    this.itemName = itemName;
}
public String getGroupName() {
    return groupName;
}
public void setGroupName(String groupName) {
    this.groupName = groupName;
}
}

Repository layer:

@Repository 
public interface ItemRepository extends PagingAndSortingRepository<Item, Long> 
{ 
 @Query("SELECT a.id, a.code, a.name AS item_name, 
 b.name as group_name from item a, item_group b 
 WHERE a.group_id = b.id AND a.id=:id")
Item findItemById(@Param("id") Long id);
}

Service layer:

@Service
public class ItemServiceImpl implements ItemService 
{   
    private final ItemRepository itemRepository;
    
    public ItemServiceImpl(ItemRepository itemRepository) 
    {
        this.itemRepository = itemRepository;
    }
    
    @Override
    @Transactional(readOnly=true)
    public Item findItemById(Long id) 
    {
        return itemRepository.findItemById(id);
    }
}

My updated main Configuration class in response to answer of Jens:

@SpringBootApplication
@EnableJdbcRepositories
public class SpringDataJdbcApplication extends AbstractJdbcConfiguration
{
    public static void main(String[] args) 
    {
        SpringApplication.run(SpringDataJdbcApplication.class, args);
    }
    
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() 
    {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        return dataSourceBuilder.build();
    }
    
    @Bean
    NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) 
    { 
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    PlatformTransactionManager transactionManager() 
    { 
        return new DataSourceTransactionManager(dataSource());
    }
}

Solution

  • If you don't get any exceptions you should be fine. There shouldn't be anything in Spring Data JDBC that silently breaks when the id is not specified.

    The problem is though: I don't consider it a feature that this works, but just accidental behaviour. This means it might break with any version, although replacing these methods with custom implementations based on a NamedParameterJdbcTemplate shouldn't be to hard, so the risk is limited.

    The question though is: Why don't you add the @Id annotation, after all your entity does have an id. And the whole idea of a repository conceptually requires an id.