代码之家  ›  专栏  ›  技术社区  ›  Tomas

Hibernate CollectionFements不会在SQL server中自动创建表

  •  0
  • Tomas  · 技术社区  · 16 年前

    使用自动更新(hibernate.hbm2ddl.auto=update),我有一个实体支持创建两个表: myentity和MyEntityConfiguration属性。 这在MySQL5中运行良好,但在SQLServer2005中它不创建属性表。

    有人知道这件事吗?我没有尝试过自己创建表,我想避免这种情况,所以我不知道它是否在SQL server中工作,但在Mysql中工作良好。

    这是实体(简化):

    @Entity
    public class MyEntity  {
        Map<String, String> configurations = new HashMap<String, String>();
        long id = -1;
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        @CollectionOfElements(fetch = FetchType.EAGER)
        @JoinTable(
                name = "myentityconfigurationProperties",
                joinColumns = @JoinColumn(name = "id")
        )
        @org.hibernate.annotations.MapKey(
                columns = @Column(name = "propertyKey")
        )
        @Column(name = "propertyValue", nullable = false)
        @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
        public Map<String, String> getConfigurations() {
            return configurations;
        }
    
        public void setConfigurations(Map<String, String> configurations) {
            this.configurations = configurations;
        }
    
    }
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Tomas    16 年前

    错误在于SQLServer不允许空键,MySQL显然会处理空键。 因此,解决办法是:

    @org.hibernate.annotations.MapKey(
            columns = @Column(name = "propertyKey", nullable = false)