使用Hibernate中的注释映射表的问题

时间:2017-04-27 19:14:48

标签: mysql spring hibernate-mapping hibernate-annotations

从这么多天来,我一直在使用注释在Hibernate中映射表。但是我无法解决或发现代码中的问题。我正在使用条件HQl Query通过注释创建实体来获取数据。  下面显示了如何构建表格。

下面是映射表 enter image description here

PK - 主键,非空和自动增量 FK - 外键,非空

SQL Dta示例enter image description here

这里每个question_id有多个选项(答案,即answer_id),每个answer_id都有2个语言的answer_text。每个question_id都有2种语言的question_text。问题分为4个科目。

Subject.java

 @Entity  
@Table(name = "SUBJECT")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

@JsonIgnoreProperties(ignoreUnknown = true)
public class Subject {

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

    @Column(name = "SUBJECT_NAME")
    private String      subjectName;

Question.Java

@Entity  
@Table(name = "QUESTION")

@JsonIgnoreProperties(ignoreUnknown = true)
public class Question {
    @Id  
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "QUESTION_ID")  
    private Long        questionId;

    @Column(name = "ANSWER_ID")
    private String      answerId;

    @Column(name = "SUBJECT_ID")
    private Long    subjectId;

    @Column(name = "QUESTION_TEST_TYPE")
    private String questionTestType;

    @Column(name = "BATCH_ID")
    private Long batchId;

    @OneToMany(fetch = FetchType.EAGER,mappedBy="question")
    @Fetch(FetchMode.SELECT)
    private Set<QuestionText> questionTexts = new HashSet<QuestionText>(0);

    @OneToMany(fetch = FetchType.EAGER,mappedBy="question")
    @Fetch(FetchMode.SELECT)
    private Set<Answer> answers = new HashSet<Answer>(0);

Answer.java

@Entity  
@Table(name = "ANSWER")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

@JsonIgnoreProperties(ignoreUnknown = true)
public class Answer {

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

    @ManyToOne
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name="QUESTION_ID",nullable=false)
    private Question question;

    @OneToMany(fetch = FetchType.EAGER,mappedBy="answer")
    @Fetch(FetchMode.SELECT)
    private Set<AnswerText> answerText = new HashSet<AnswerText>(0);    

QuestionText.java

@Entity  
@Table(name = "QUESTIONTEXT")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)



  @JsonIgnoreProperties(ignoreUnknown = true)
    public class QuestionText {

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

        @Column(name = "QUESTION_ID",insertable=false,updatable=false)
        private Long        questionId;

        @Column(name = "QUESTION_TEXT")
        private String      questionText;

@ManyToOne
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name="LANGUAGE_ID",nullable=false)
    private Language language;


    @ManyToOne
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name="QUESTION_ID",nullable=false)
    private Question question;

AnswerText.java

@Entity  
@Table(name = "ANSWERTEXT")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

@JsonIgnoreProperties(ignoreUnknown = true)
public class AnswerText {

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

    @Column(name = "ANSWER_ID",insertable=false,updatable=false)  
    private Long answerId;

    @Column(name = "ANSWER_TEXT")  
    private String answerText;

    @ManyToOne
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name="LANGUAGE_ID",nullable=false)
    private Language language;

    @ManyToOne
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name="ANSWER_ID",nullable=false)
    private Answer answer;

Language.java

@Entity  
@Table(name = "LANGUAGE")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)


@JsonIgnoreProperties(ignoreUnknown = true)
public class Language {

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

    @Column(name = "LANGUAGE_CODE")
    private String      languageCode;

    @Column(name = "TERRITORY")
    private String      territory;

    @OneToMany(fetch = FetchType.LAZY,mappedBy="language")
    @Fetch(FetchMode.SELECT)
    private Set<AnswerText> answerText = new HashSet<AnswerText>();

    @OneToMany(fetch = FetchType.LAZY,mappedBy="language")
    @Fetch(FetchMode.SELECT)
    private Set<QuestionText> questionText = new HashSet<QuestionText>();

面临的问题是:

1。)我的数据库中有2种语言,问题是我为西班牙语答案提出英语问题,反之亦然,或者有些时间显示西班牙语而不是英语。但是在DB中,我只有英语答案的英语问题和西班牙语的西班牙语答案。

以下是问题:

1.)在quesitonText表中我没有将question_id设置为FK,这是否会影响映射。

2.)在问题表中,batch_id是某些其他表的PK,我应该将其设置为FK吗?这里batch_id可以是Null。这会影响任何映射吗?

任何机构都可以指出我的代码中出了什么问题。所以它显示了西班牙语和英语的混合。请帮我解决这个问题。请

1 个答案:

答案 0 :(得分:0)

我可能误解了您的设置,但我希望QuestionText包含

@ManyToOne
@Fetch(FetchMode.SELECT)
@JoinColumn(name="LANGUAGE_ID",nullable=false)
private Language language;

您甚至可以在Language

中使用此功能
@OneToMany(fetch = FetchType.LAZY,mappedBy="language")
@Fetch(FetchMode.SELECT)
private Set<QuestionText> questionText = new HashSet<QuestionText>();

如果您可以发布正确获取特定语言的所有问题和答案的SQL,那么了解数据库的结构将会很有帮助。

关于您的ER图的其他一些想法: 当答案表引用question_id时,为什么问题表引用了answer_id?你可以删除问题表中的answer_id字段......?