Hibernate MS SQL 连接问题

发布于 2024-08-23 01:35:56 字数 3784 浏览 9 评论 0原文

我在客户端 mssql 数据库中有两个表。第一个是作业表 - 所以我创建了一个作业实体,其中包含负载类型和负载重量以及所有这些内容 - 工作正常。

我现在的问题是还有第二个表,其中包含有关加载和卸载点的信息。第二个表,我称之为 JEP,有一个由几个项目组成的主键:类型(装载或卸载)、邮政编码和客户编号。

我创建了一个实体 JobEndPoint,NetBeans 还创建了一个代表包含所有这些字段的主键 JobEndPointPK 的对象。

我想向我的作业实体添加两个 JobEndPoint(loadPoint 和 unloadPoint)。我现在的问题是:如何在 Hibernate 中对其进行注释?在我看来,这是一个@OneToOne 关系。如果我可以指定像 SELECT * FROM JEP WHERE type="load" AND customer_nr="123" AND zip_code="123 ..." 这样的 SELECT 语句,那就完美了。 Hibernate 可以做到这一点吗?

感谢您的帮助!

此致,

Marco


以下是实体:

@Entity
@Table(name = "Auftragsdaten", catalog = "...", schema = "dbo")
public class Job implements Comparable<Object>, Serializable {

    private static final long serialVersionUID = 4285871251915951149L;

    @Id
    @Basic(optional = false)
    @Column(name = "`id`", nullable = false)
    int id;

    @Column(name = "`AufNr`", nullable=false)
    int jobId;

    @Transient
    List<Integer> jobsAdded;

    @Column(name = "`Beladedatum`", nullable=false)
    @Temporal(TemporalType.DATE)
    Date loadDate;

    @Column(name = "`Beladezeit`")
    @Temporal(TemporalType.TIME)
    Date loadTimeFrom;

    @Transient
    Date loadTimeTo;

    @Column(name = "`Entladedatum`", nullable=false)
    @Temporal(TemporalType.DATE)
    Date unloadDate;

    @Column(name = "`Entladezeit Beginn`")
    @Temporal(TemporalType.TIME)
    Date unloadTimeFrom;

    @Column(name = "`Entladezeit Ende`")
    @Temporal(TemporalType.TIME)
    Date unloadTimeTo;

    @Transient
    List<JobEndPoint> froms;

    @OneToOne
    @JoinColumns ({
        @JoinColumn(name="`Beladetyp`", referencedColumnName = "`Ladetyp`", insertable = false, updatable = false),
        @JoinColumn(name="`AbsNr`", referencedColumnName = "`KundenNr`", insertable = false, updatable = false),
        @JoinColumn(name="`Verkehrsart`", referencedColumnName = "`VerkArt`", insertable = false, updatable = false),
        @JoinColumn(name="`von LKZ`", referencedColumnName = "`LKZ`", insertable = false, updatable = false),
        @JoinColumn(name="`von PLZ`", referencedColumnName = "`PLZ`", insertable = false, updatable = false)
    })
    JobEndPoint fromPoint;

    @Transient
    JobEndPoint toPoint;

    @Column(name = "`Verkehrsart`", length = 10, nullable=false)
    @Enumerated
    JobType type;

    @Column(name = "`Anzahl Paletten CCG1`")
    int numberCCG1;

    @Column(name = "`Anzahl Paletten CCG2`")
    int numberCCG2;

    @Transient
    int numberFullContainer;

    @Transient
    int numberEmptyContainer;

    @Column(name = "`Anzahl Container`")
    int numberContainer;

    @Column(name = "`Anz Stellplätze`")
    int numberUnits;

    @Column(name = "`Bruttogewicht`", nullable=false)
    int loadWeight;

    @ManyToOne
    @JoinColumn(name="`Kühlkennzeichen`")
    CoolingCode coolingCode;
}

@Entity
@Table(name = "BES", catalog = "...", schema = "dbo")
public class JobEndPoint implements Serializable {

    private static final long serialVersionUID = 1017986852824783744L;

    @Id
    protected JobEndPointPK jobEndPointPK;

    (...)
}

@Embeddable
public class JobEndPointPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "`Ladetyp`", nullable = false, length = 50)
    @Enumerated
    EndPointType type;

    @Basic(optional = false)
    @Column(name = "`KundenNr`", nullable = false)
    int customerId;

    @Basic(optional = false)
    @Column(name = "`VerkArt`", nullable = false, length = 10)
    @Enumerated
    JobType jobType;

    @Basic(optional = false)
    @Column(name = "`LKZ`", nullable = false, length = 3)
    String countryCode;

    @Basic(optional = false)
    @Column(name = "`PLZ`", nullable = false, length = 7)
    String zipCode;
}

I have two tables in the clients mssql database. The first is a job table - so I created an Job entity which contains the load type and load weight and all that stuff - works fine.

My problem now is that there is a second table that includes informations about the load and unload point. The second table, I call it JEP, has a primary key consisting of several items: the type (load or unload), the zip code and the customer number.

I created an entity JobEndPoint and NetBeans also created an object representing the primary key JobEndPointPK containing all that fields.

I want to add two JobEndPoint (loadPoint and unloadPoint) to my Job entity. My problem is now: how do I annotate that in Hibernate? In my opinion it is an @OneToOne relation ship. It would be perfect if I could specify a SELECT statement like SELECT * FROM JEP WHERE type="load" AND customer_nr="123" AND zip_code="123 ...". Is that possible with Hibernate?

Thanks for your help!

Regeards,

Marco


Here are the Entities:

@Entity
@Table(name = "Auftragsdaten", catalog = "...", schema = "dbo")
public class Job implements Comparable<Object>, Serializable {

    private static final long serialVersionUID = 4285871251915951149L;

    @Id
    @Basic(optional = false)
    @Column(name = "`id`", nullable = false)
    int id;

    @Column(name = "`AufNr`", nullable=false)
    int jobId;

    @Transient
    List<Integer> jobsAdded;

    @Column(name = "`Beladedatum`", nullable=false)
    @Temporal(TemporalType.DATE)
    Date loadDate;

    @Column(name = "`Beladezeit`")
    @Temporal(TemporalType.TIME)
    Date loadTimeFrom;

    @Transient
    Date loadTimeTo;

    @Column(name = "`Entladedatum`", nullable=false)
    @Temporal(TemporalType.DATE)
    Date unloadDate;

    @Column(name = "`Entladezeit Beginn`")
    @Temporal(TemporalType.TIME)
    Date unloadTimeFrom;

    @Column(name = "`Entladezeit Ende`")
    @Temporal(TemporalType.TIME)
    Date unloadTimeTo;

    @Transient
    List<JobEndPoint> froms;

    @OneToOne
    @JoinColumns ({
        @JoinColumn(name="`Beladetyp`", referencedColumnName = "`Ladetyp`", insertable = false, updatable = false),
        @JoinColumn(name="`AbsNr`", referencedColumnName = "`KundenNr`", insertable = false, updatable = false),
        @JoinColumn(name="`Verkehrsart`", referencedColumnName = "`VerkArt`", insertable = false, updatable = false),
        @JoinColumn(name="`von LKZ`", referencedColumnName = "`LKZ`", insertable = false, updatable = false),
        @JoinColumn(name="`von PLZ`", referencedColumnName = "`PLZ`", insertable = false, updatable = false)
    })
    JobEndPoint fromPoint;

    @Transient
    JobEndPoint toPoint;

    @Column(name = "`Verkehrsart`", length = 10, nullable=false)
    @Enumerated
    JobType type;

    @Column(name = "`Anzahl Paletten CCG1`")
    int numberCCG1;

    @Column(name = "`Anzahl Paletten CCG2`")
    int numberCCG2;

    @Transient
    int numberFullContainer;

    @Transient
    int numberEmptyContainer;

    @Column(name = "`Anzahl Container`")
    int numberContainer;

    @Column(name = "`Anz Stellplätze`")
    int numberUnits;

    @Column(name = "`Bruttogewicht`", nullable=false)
    int loadWeight;

    @ManyToOne
    @JoinColumn(name="`Kühlkennzeichen`")
    CoolingCode coolingCode;
}

@Entity
@Table(name = "BES", catalog = "...", schema = "dbo")
public class JobEndPoint implements Serializable {

    private static final long serialVersionUID = 1017986852824783744L;

    @Id
    protected JobEndPointPK jobEndPointPK;

    (...)
}

@Embeddable
public class JobEndPointPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "`Ladetyp`", nullable = false, length = 50)
    @Enumerated
    EndPointType type;

    @Basic(optional = false)
    @Column(name = "`KundenNr`", nullable = false)
    int customerId;

    @Basic(optional = false)
    @Column(name = "`VerkArt`", nullable = false, length = 10)
    @Enumerated
    JobType jobType;

    @Basic(optional = false)
    @Column(name = "`LKZ`", nullable = false, length = 3)
    String countryCode;

    @Basic(optional = false)
    @Column(name = "`PLZ`", nullable = false, length = 7)
    String zipCode;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-08-30 01:35:56

一般来说,我建议使用生成的内部主键而不是复合键。但是,如果您需要坚持使用复合键,这里有一些想法希望能有所帮助。

我知道 JobEndPointPK 是作为标识符组件实现的(请参阅 Hibernate 参考,第 8.4 章)。注意:正确实现 equals 和 hashCode 方法至关重要,因为 Hibernate 依赖于这些方法。

更新:假设您的JobEndPointJobEndPointPK看起来像这样:

@Embeddable
class JobEndPointPK {
    @Column(name = "type", nullable = false)
    @Enumerated
    EndPointType type;

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

    @Column(name = "customerNumber", nullable = false)
    int customerId;

    // equals, hasCode, getters, setters etc.
}

@Entity
class JobEndPoint {
    @Id
    private JobEndPointPK key;

    // getters, setters etc.
}

映射注释将类似于:

@Entity
class Job {
    @OneToOne
    @JoinColumns ({
        @JoinColumn(name="loadPointType", referencedColumnName = "type"),
        @JoinColumn(name="loadPointZip", referencedColumnName = "zipCode"),
        @JoinColumn(name="loadPointCust", referencedColumnName = "customerNumber")
    })
    private JobEndPoint loadPoint;
    // similarly for unloadPoint
    // other properties
}

该示例改编自此处

我不确定如何处理 JobEndPointPK.type,但对于 loadPoint 来说,它显然是 Load ,而对于 unloadPoint >、Unload,因此您很可能不想将其单独存储在数据库中。我的猜测是,您可以使用 @Formula 注释指定值,但我还没有看到任何具体的示例。

请注意,所有这些代码纯粹是实验性的,我还没有测试过。

该主题还有其他变体。有关更多详细信息,请参阅第 8 章中的“带有注释的复合键”部分。 Java 持久化与 Hibernate 的结合。

In general, I would recommend using a generated internal primary key instead of the composite key. However, if you need to stick with your composite key, here are some ideas that hopefully help.

I understand that JobEndPointPK is implemented as an identifier component (see the Hibernate Reference, chapter 8.4). Note: it is critical that it implements the equals and hashCode` methods correctly, as Hibernate relies on these.

Updated: Provided that your JobEndPoint and JobEndPointPK looks something like this:

@Embeddable
class JobEndPointPK {
    @Column(name = "type", nullable = false)
    @Enumerated
    EndPointType type;

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

    @Column(name = "customerNumber", nullable = false)
    int customerId;

    // equals, hasCode, getters, setters etc.
}

@Entity
class JobEndPoint {
    @Id
    private JobEndPointPK key;

    // getters, setters etc.
}

The mapping annotation would be something like:

@Entity
class Job {
    @OneToOne
    @JoinColumns ({
        @JoinColumn(name="loadPointType", referencedColumnName = "type"),
        @JoinColumn(name="loadPointZip", referencedColumnName = "zipCode"),
        @JoinColumn(name="loadPointCust", referencedColumnName = "customerNumber")
    })
    private JobEndPoint loadPoint;
    // similarly for unloadPoint
    // other properties
}

The example is adapted from here.

I am not sure how to deal with JobEndPointPK.type though, as for loadPoint it is obviously Load and for unloadPoint, Unload, so you most probably don't want to store it separately in the DB. My gues is that you can specify the value with the @Formula annotation, but I haven't seen any concrete example for this.

Note that all this code is purely experimental, I haven't tested it.

There are other variations on the theme. For more details, see the section "Composite keys with annotations" in Chapter 8 of Java Persistence with Hibernate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文