1

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());
    }
}
Ammamon
  • 467
  • 1
  • 10
  • 18

2 Answers2

0

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Thank you Jens for pointing out the issues. I shall use @Id and NamedParameterJdbcTemplate as you suggested. – Ammamon Nov 04 '20 at 05:44
  • Jens, please verify whether the only thing I need to do in using NamedParameterJdbcTemplate is to have my bean configuration as provided in my edited question. I hope we need not use directly any of those configured beans, thereby retaining my repository code as it is. I read in one of your blogs that NamedParameterJdbcOperations is used only internally to submit SQL statements to the database. – Ammamon Nov 04 '20 at 07:48
  • Jens, since the application works even without these configured beans, I assume that they serve only to reduce the risk of breaking with any version change as you mentioned in your answer. – Ammamon Nov 04 '20 at 07:54
0

If it's working and you really don't want to use the annotations, you can do it. But I think that it's unnecessary complication. You can expect errors that would not be there if you had used the annotations and code will be harder to debug. If you are new in Spring I recommend to use annotations. But after all it depend on you how will you design your applications. For sure advantage of approach without annotations is higher control about database.

fr3ddie
  • 406
  • 6
  • 17