【问题标题】:How will HQL(hibernate query language) need to change if we use @embedded annotataion?如果我们使用@embedded 注解,HQL(hibernate 查询语言)需要如何改变?
【发布时间】:2018-07-23 06:45:31
【问题描述】:

spring mvc + 休眠
我有两个包含用户数据的表。
现在我想上两节课。
1. 用户
2. 地址 -> 因为我们可以在很多地方使用它。
我们在这方面有关系。

@Embedded
class Address

class user
{
@Autowired
Address address;
}

这是个好主意还是坏主意?

因此我可以更改休眠查询吗?

【问题讨论】:

    标签: java hibernate spring-mvc spring-boot spring-data-jpa


    【解决方案1】:

    实现has-a关系是个好主意。

    使用可嵌入注释创建类地址以定义公共列(属性)

    @Embeddable
    class Address{
        String city;    
        String state;
    }
    

    在 User 类中使用 has-a 关系

    public class User {
        @Embedded
        private Address address;
    }
    

    你可以这样查询:

    session.createQuery( "select u from User u where u.address.city=:city" ).setParameter( "city", 'ahmedabad' ).list();
    

    或:

        session.createQuery( "select u from User u where u.address.city in (:city)" ).setParameterList( "city", Arrays.asList( 'surat', 'ahmedabad' ) ).list();
    

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2014-10-16
      • 2013-05-29
      • 2012-03-12
      • 1970-01-01
      相关资源
      最近更新 更多