简体   繁体   English

如果我在 Spring Data JDBC 中使用没有 @Id 的实体类,我是否遗漏了什么?

[英]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我刚刚尝试在 Spring Data JDBC 中成功使用没有 @Id 的实体类

Custom query was added in my repository for retrieving data from 2 mysql tables and returning an entity having the joined table data.在我的存储库中添加了自定义查询,用于从 2 个 mysql 表中检索数据并返回具有连接表数据的实体。

If I plan to use only custom queries, am I missing anything here?如果我打算只使用自定义查询,我在这里遗漏了什么吗?

Here's my entity class without @Id or @Entity:这是我没有 @Id 或 @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:响应 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());
    }
}

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.当未指定 id 时,Spring Data JDBC 中不应该有任何内容会悄悄中断。

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.这意味着它可能会因任何版本而中断,尽管用基于NamedParameterJdbcTemplate自定义实现替换这些方法应该不会太难,因此风险是有限的。

The question though is: Why don't you add the @Id annotation, after all your entity does have an id.但问题是:为什么不添加@Id注释,毕竟您的实体确实有一个 id。 And the whole idea of a repository conceptually requires an id.从概念上讲,存储库的整个想法需要一个 id。

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.如果您是 Spring 新手,我建议您使用注释。 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.没有注释的方法的肯定优势是对数据库的更高控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM