使用hibernate auto-ddl创建表

时间:2018-04-09 10:56:40

标签: hibernate jpa spring-data-jpa

我正在使用spring引导开发一个应用程序并使用hibernate来持久化。我已设置spring.jpa.hibernate.ddl-auto=update。但是数据库中创建的表不正确。这些是我的3个实体类

@Entity
@Table(name = "tContent")
public class Content extends BaseEntity implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "Content_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "Title")
    private String title;

    @Column(name = "Description")
    private String Description;

    @OneToMany
    @JoinTable(
            name = "tContentTag", 
            joinColumns = @JoinColumn(name = "Content_ID", referencedColumnName = "Content_ID"),
            inverseJoinColumns =@JoinColumn(name="Tag_ID", referencedColumnName = "Tag_ID")
            )
    private Set<Tag> tags;

    public Content() {
    }

}

@Entity
@Table(name="tTag")
public class Tag extends BaseEntity implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "Tag_ID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Column(name = "Tag_Name", nullable = false, columnDefinition = "text character set utf8 collate utf8_unicode_ci")
    private String tagName ;

    public Tag() {}

    public Tag(String tagName) {
        this.tagName = tagName ;
    }

    public String getTagName() {
        return tagName;
    }


}

@Entity
@Table(name = "tContentTag")
public class ContentTag implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "Content_ID", unique = false)
    private Content content;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "Tag_ID", unique=false)
    private Tag tag;
}

这就是在数据库中创建表的方法

CREATE TABLE `t_content` (
  `content_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `description` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`content_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;

CREATE TABLE `t_tag` (
  `tag_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `created_on` datetime DEFAULT CURRENT_TIMESTAMP,
  `tag_name` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;

CREATE TABLE `t_content_tag` (
  `id` bigint(20) NOT NULL,
  `content_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `tag_id` bigint(20) NOT NULL,
  PRIMARY KEY (`content_id`,`tag_id`),
  UNIQUE KEY `UK_33lrnpl9s4uomdag4n2iftghp` (`tag_id`),
  CONSTRAINT `FKggjqilc3bfp75f3bqne5qlill` FOREIGN KEY (`tag_id`) REFERENCES `t_tag` (`tag_id`),
  CONSTRAINT `FKjwge9erpywu6jvn5thsixrabt` FOREIGN KEY (`content_id`) REFERENCES `t_content` (`content_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

t_Content_Tag表中的列Content_ID不是自动递增,同一表中的id是自动递增但不是作为自动递增coulmn创建的。如何正确创建这些表。 t_Content_Tag表是一个链接表。

0 个答案:

没有答案