简体   繁体   中英

Hibernate Unidirectional OneToMany Not Working with composite id

I've been bashing my head on my keyboard for two days trying to figure this out...

Some background: We have a data model set up with a Perl code base that runs straight native SQL statements to the database via ODBC. For certain reasons, we decided to rewrite the code in Java... I thought it would be a good idea to use Hibernate to define all of the mappings. We don't want to edit the data model.

For simplicity sake, I can express the problem with only part of our data model. We have the entities "Job","JobDatabase" and "JobTable".

Job has a PK of job_name. Database has a PK of job_name,name. Table has a PK of job_name,src_database_name,name. As you may expect, Job has a OneToMany relationship with JobDatabase, and Database has a OneToMany with JobTable.

For purposes of this test, I'm starting with empty tables and trying to create some sample data. I can insert a Job and a JobDatabase, but when I try to insert the JobTable, Hibernate throws an error. Or more accurately, that is where it complains. It doesn't start executing my code because it detects the mapping error. However, if I remove the association between JobDatabase and JobTable, it will insert all Job and JobDatabase records correctly with no errors.

Sample Classes (all fields have getters/setters... there are also many other fields):

@Entity
@Table(name="Job")
public class Job implements Serializable {
    @Id
    @Column(name="job_name",nullable = false)
    private String jobName;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "job_name", referencedColumnName = "job_name")
    private Set<JobDatabase> databases;
}

@Entity
@Table(name="JobDatabase")
public class JobDatabase implements Serializable {
    @Id
    @Column(name="job_name",nullable = false)
    private String jobName;

    @Id
    @Column(name="name",nullable = false)
    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumns({
            @JoinColumn(name = "job_name", referencedColumnName = "job_name"),
            @JoinColumn(name = "name", referencedColumnName = "src_database_name")
    })
    private Set<JobTable> tables;
}

@Entity
@Table(name="JobTable")
public class JobTable implements Serializable{
    @Id
    @Column(name="job_name",nullable = false)
    private String jobName;

    @Id
    @Column(name="src_database_name",nullable = false)
    private String srcDatabaseName;

    @Id
    @Column(name="name",nullable = false)
    private String name;
}

The error:

Exception in thread "main" org.hibernate.MappingException: Unable to find column with logical name: src_database_name in JobDatabase

I keep getting this error. I do not understand why it is looking for the referenced column in the entity "owning" the mapping. src_database_name does indeed only exist in JobTable - it is referred to as "name" in JobDatabase. JobTable also has a "name" field, but it refers to the name of the Table.

You need to have src_database_name column in your JobDatabase table. Or you can change src_database_name to other column name.

For composite key reference column must be present in your source table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM