Hibernate:将新数据库对象的一对多引用设置为另一个对象,而不先获取现有对象

时间:2016-07-29 16:13:08

标签: java hibernate

我正在使用Hibernate构建应用程序。我使用的数据库模式包含(其中包括)两个名为using System; using System.Collections.Generic; using System.Linq; using System.Text; using iText.Layout; using iText.Layout.Borders; using iText.Layout.Element; namespace iTextTest { public static class iTextSharpHelper { public static T SetBorderEx<T>(this ElementPropertyContainer<T> element, Border border) where T : ElementPropertyContainer<T> { element.SetBorder(border); return (T)element; } public static Paragraph Style(this BlockElement<Paragraph> element) { element .SetBorderEx(iText.Layout.Borders.Border.NO_BORDER) .SetFont(iText.Kernel.Font.PdfFontFactory.CreateFont(iText.IO.Font.FontConstants.HELVETICA)) .SetFontSize(10.0f) .SetFixedLeading(12.0f) .SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.BOTTOM) .SetMargin(0f); return (Paragraph)element; } } class Program { private static float[] tableColumns = { 0.35f, 0.25f, 0.15f, 0.25f }; static void Main(string[] args) { iText.Kernel.Pdf.PdfDocument pdf = new iText.Kernel.Pdf.PdfDocument(new iText.Kernel.Pdf.PdfWriter("test.pdf")); iText.Layout.Document document = new iText.Layout.Document(pdf, iText.Kernel.Geom.PageSize.A4); document.SetMargins(50f, 50f, 25f, 50f); iText.Layout.Element.Table mainTable = new iText.Layout.Element.Table(tableColumns) .SetBorderEx(iText.Layout.Borders.Border.NO_BORDER) .SetWidthPercent(100) .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.LEFT) .SetPadding(0f); for (int i = 0; i < 10; i++) { AddRow(mainTable, "ABCDEFGHIJ", "ABCDEFGHIJ", "ABCDEFGHIJ"); } document.Add(mainTable); document.Close(); } private static void AddRow(iText.Layout.Element.Table table, string col1, string col2, string col3) { // Label AddCell(table, col1, true) .SetBorderTop(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f)); // Product - Voucher and price/pcs AddCell(table, col2, true) .SetBorderTop(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f)); // Message AddCell(table, col3, true, 2) .SetBorderTop(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f)) //.SetBorderRight(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f)) .SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT) .SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT); } private static iText.Layout.Element.Cell AddCell(iText.Layout.Element.Table table, string text, bool setBold = false, int colSpan = 1) { iText.Layout.Element.Cell cell = new iText.Layout.Element.Cell(1, colSpan) .SetBorderEx(iText.Layout.Borders.Border.NO_BORDER) .SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.BOTTOM); if (!string.IsNullOrEmpty(text)) { iText.Layout.Element.Paragraph paragraph = new iText.Layout.Element.Paragraph(text) .Style(); if (setBold) paragraph.SetBold(); cell.Add(paragraph); } table.AddCell(cell); return cell; } } } //.SetBorderRight(new iText.Layout.Borders.SolidBorder(iText.Kernel.Colors.Color.BLACK, 0.5f)) 的表。后者包含描述assignment类型的主键/值对(例如&#34;书面&#34;,&#34;实用&#34;)。

但是,

assignment_type是一个由assignmentassignment等类扩展的抽象类。为此,我使用类表继承方法(some details on the topic)。

现在,在创建像written_assignment这样的类的实例期间,在practical_assignment中设置对相应对象的引用是有意义的,如下所示:

assignment_type

据我了解,使用&#34;创建实例&#34;方法只能工作一次,一次是数据库中没有对象的状态(因为written_assignment表的值被设置为唯一的)。

有没有办法在public WrittenAssignment() { AssignmentType assignmentType = ...; // Create or acquire appropriate instance this.setAssignmentType(assignmentType); } 的构造函数中设置assignment_type对象的引用而不先获取它?奖金问题:如果不存在匹配对象,是否可以创建?

1 个答案:

答案 0 :(得分:1)

您可以使用引用assignment_type表中assignment表的列作为鉴别器列,然后将AssignmentType关系映射为只读@ManyToOne,将insertableupdatable设置为false

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="ASSIGNMENT_TYPE_ID")
@Table(name="assignment")
public abstract class Assignment {
    ...
    @ManyToOne
    @JoinColumn(name = "ASSIGNMENT_TYPE_ID", insertable = false, updatable = false)
    private AssignmentType assignmentType;
}

在子类中,将适当的鉴别器列值与@DiscriminatorValue注释放在一起:

@Entity
@DiscriminatorValue("W")
@Table(name="written_assignment")
public class WrittenAssignment extends Assignment {
    ...
}

当您需要创建新的子实体时,请使用构造函数,并且由于子实体鉴别器而隐式赋值类型。当您读取现有实体实例并获取关联的分配类型时,将加载相应的分配类型。