在Hibernate中保存实例时出现SQL错误

时间:2014-04-30 13:37:24

标签: java sql hibernate

当我尝试保存实例时,我收到了这个奇怪的错误:

WARN [15:06:27,917] JDBCExceptionReporter - SQL Error: 20000, SQLState: 42X04
ERROR[15:06:27,917] JDBCExceptionReporter - Column 'ad0b8d24-f596-47cb-9d79-06a3c9c1de26' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE  statement then 'ad0b8d24-f596-47cb-9d79-06a3c9c1de26' is not a column in the target table.

行未插入数据库。 看起来它正在尝试使用scenario_id(uuid)作为列名。但为什么呢?

我正在使用此数据访问对象:

public interface ScenarioDao extends GenericDao<Scenario, String> {
    public List<Scenario> getScenariosWhereOwner(Person owner);
    public List<Scenario> getScenariosWhereOwner(Person person, int LIMIT);
...
}

public interface GenericDao <T, PK extends Serializable>{
  public PK create(T newInstance) {
    PK primaryKey = (PK) getHibernateTemplate().save(newInstance);
    return primaryKey;
}
}

POJO:

@Entity
@Table(name = "SCENARIO")
@XmlRootElement
public class Scenario implements Serializable, Comparable<Scenario> {

    private static final long serialVersionUID = -6608175331606366993L;

    private String scenarioId;
    private Person person;
    private ResearchGroup researchGroup;
    private String title;
    private int scenarioLength;
    private boolean privateScenario;
    private String description;
    private String scenarioName;
    private String mimetype;
    private Set<History> histories = new HashSet<History>(0);
    private Set<Experiment> experiments = new HashSet<Experiment>(0);
    private boolean userMemberOfGroup;
    private Blob scenarioFile;
    private String group;
    private Boolean availableFile;

    private InputStream fileContentStream;

    @Transient
    public boolean isUserMemberOfGroup() {
        return userMemberOfGroup;
    }

    public void setUserMemberOfGroup(boolean userMemberOfGroup) {
        this.userMemberOfGroup = userMemberOfGroup;
    }

    @Transient
    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    @Transient
    public Boolean getAvailableFile() {
        return availableFile;
    }

    public void setAvailableFile(Boolean availableFile) {
        this.availableFile = availableFile;
    }

    public Scenario() {
    }

    public Scenario(Person person, ResearchGroup researchGroup) {
        this.person = person;
        this.researchGroup = researchGroup;
    }

    public Scenario(Person person, ResearchGroup researchGroup, String title,
            int scenarioLength, boolean privateScenario, String description,
            String scenarioName, String mimetype, Set<History> histories,
            Set<Experiment> experiments) {
        this.person = person;
        this.researchGroup = researchGroup;
        this.title = title;
        this.scenarioLength = scenarioLength;
        this.privateScenario = privateScenario;
        this.description = description;
        this.scenarioName = scenarioName;
        this.mimetype = mimetype;
        this.histories = histories;
        this.experiments = experiments;
    }

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @Column(name = "SCENARIO_ID", nullable = false, length = 36, scale = 0)
    public String getScenarioId() {
        return this.scenarioId;
    }

    public void setScenarioId(String scenarioId) {
        this.scenarioId = scenarioId;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "OWNER_ID", nullable = false)
    public Person getPerson() {
        return this.person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "RESEARCH_GROUP_ID", nullable = false)
    public ResearchGroup getResearchGroup() {
        return this.researchGroup;
    }

    public void setResearchGroup(ResearchGroup researchGroup) {
        this.researchGroup = researchGroup;
    }

    @Column(name = "TITLE", unique = true)
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name = "SCENARIO_LENGTH", precision = 22, scale = 0)
    public int getScenarioLength() {
        return this.scenarioLength;
    }

    public void setScenarioLength(int scenarioLength) {
        this.scenarioLength = scenarioLength;
    }

    @Column(name = "PRIVATE", precision = 1, scale = 0)
    public boolean isPrivateScenario() {
        return this.privateScenario;
    }

    public void setPrivateScenario(boolean privateScenario) {
        this.privateScenario = privateScenario;
    }

    @Lob
    @Type(type = "org.hibernate.type.TextType")
    @Column(name = "DESCRIPTION")
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "SCENARIO_NAME")
    public String getScenarioName() {
        return this.scenarioName;
    }

    public void setScenarioName(String scenarioName) {
        this.scenarioName = scenarioName;
    }

    @Column(name = "MIMETYPE")
    public String getMimetype() {
        return this.mimetype;
    }

    public void setMimetype(String mimetype) {
        this.mimetype = mimetype;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "scenario")
    public Set<History> getHistories() {
        return this.histories;
    }

    public void setHistories(Set<History> histories) {
        this.histories = histories;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "scenario")
    public Set<Experiment> getExperiments() {
        return this.experiments;
    }

    public void setExperiments(Set<Experiment> experiments) {
        this.experiments = experiments;
    }

    @XmlJavaTypeAdapter(BlobSerializer.class)
    @Basic(fetch = FetchType.LAZY)
    @Lob
    @Column(name = "SCENARIO_FILE", nullable = true)
    public Blob getScenarioFile() {
        return this.scenarioFile;
    }

    public void setScenarioFile(Blob scenarioFile) {
        this.scenarioFile = scenarioFile;
    }

    @Override
    public int compareTo(Scenario scen) {
        return this.title.compareTo(scen.getTitle());
    }

    public void setFileContentStream(InputStream inputStream) {
        this.fileContentStream = inputStream;
    }

    @Transient
    public InputStream getFileContentStream() {
        return fileContentStream;
    }
}

我尝试使用以下代码创建它:

 scenario = new Scenario();
            scenario.setPrivateScenario(some boolean);
            scenario.setScenarioLength(some int);
            scenario.setDescription(some string);
            scenario.setTitle(some string);
            scenario.setResearchGroup(some ResearchGroup);
            scenario.setPerson(some Person);

正确设置所有这些参数。我也使用GenericDao与其他对象没有错误。这是由hibernate生成的insert语句:

DEBUG[16:07:25,132] SQL - insert into SCENARIO (DESCRIPTION, MIMETYPE, OWNER_ID, PRIVATE, RESEARCH_GROUP_ID, SCENARIO_FILE, SCENARIO_LENGTH, SCENARIO_NAME, TITLE, SCENARIO_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
TRACE[16:07:25,153] BasicBinder - binding parameter [1] as [LONGVARCHAR] - newscenariodescription
TRACE[16:07:25,153] BasicBinder - binding parameter [2] as [VARCHAR] - <null>
TRACE[16:07:25,153] BasicBinder - binding parameter [3] as [VARCHAR] - 9e87924e-3a14-4f82-ad57-c191ead873b5
TRACE[16:07:25,153] BasicBinder - binding parameter [4] as [BIT] - false
TRACE[16:07:25,154] BasicBinder - binding parameter [5] as [VARCHAR] - b399f04f-92a7-427c-9af5-f90055cb1ddc
TRACE[16:07:25,154] BasicBinder - binding parameter [6] as [BLOB] - <null>
TRACE[16:07:25,154] BasicBinder - binding parameter [7] as [INTEGER] - 5
TRACE[16:07:25,154] BasicBinder - binding parameter [8] as [VARCHAR] - <null>
TRACE[16:07:25,154] BasicBinder - binding parameter [9] as [VARCHAR] - newscenario
TRACE[16:07:25,154] BasicBinder - binding parameter [10] as [VARCHAR] - 2d71bcd2-756e-4ffd-82b0-9649d7f05e0b
WARN [16:07:25,180] JDBCExceptionReporter - SQL Error: 20000, SQLState: 38000
ERROR[16:07:25,180] JDBCExceptionReporter - The exception 'java.sql.SQLException: Column '2d71bcd2-756e-4ffd-82b0-9649d7f05e0b' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE  statement then '2d71bcd2-756e-4ffd-82b0-9649d7f05e0b' is not a column in the target table.' was thrown while evaluating an expression.

当我尝试执行时:

String query = "insert into SCENARIO (DESCRIPTION, MIMETYPE, OWNER_ID, PRIVATE, RESEARCH_GROUP_ID, FILE_CONTENT, SCENARIO_LENGTH, SCENARIO_NAME, TITLE, SCENARIO_ID) values ('newscenariodescription', NULL, '9e87924e-3a14-4f82-ad57-c191ead873b5', 0, 'b399f04f-92a7-427c-9af5-f90055cb1ddc', NULL, 5, NULL, 'text', '2d71bcd2-756e-4ffd-82b0-9649d7f205e0b')";

session.createSQLQuery(query).executeUpdate();

我得到同样的错误。

当我尝试直接执行查询时,我得到一个非常奇怪的错误:

SQL Error [20000] [38000]: The exception 'java.lang.NoClassDefFoundError: org/jumpmind/symmetric/db/derby/DerbyFunctions' was thrown while evaluating an expression.
SQL Error [XJ001]: Java exception: 'org/jumpmind/symmetric/db/derby/DerbyFunctions: java.lang.NoClassDefFoundError'.
  The exception 'java.lang.NoClassDefFoundError: org/jumpmind/symmetric/db/derby/DerbyFunctions' was thrown while evaluating an expression.
    The exception 'java.lang.NoClassDefFoundError: org/jumpmind/symmetric/db/derby/DerbyFunctions' was thrown while evaluating an expression.
      Java exception: 'org/jumpmind/symmetric/db/derby/DerbyFunctions: java.lang.NoClassDefFoundError'.
        org/jumpmind/symmetric/db/derby/DerbyFunctions

1 个答案:

答案 0 :(得分:0)

我忘记在触发器表中设置capture_big_lobs。将值更改为1解决了问题。但是,如果列为NULL,则SymmetricDS将抛出Null Pointer Exception。我使用的是3.5.10,所以也许它在新版本中得到了解决。