我正在使用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
是一个由assignment
和assignment
等类扩展的抽象类。为此,我使用类表继承方法(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
对象的引用而不先获取它?奖金问题:如果不存在匹配对象,是否可以创建?
答案 0 :(得分:1)
您可以使用引用assignment_type
表中assignment
表的列作为鉴别器列,然后将AssignmentType
关系映射为只读@ManyToOne
,将insertable
和updatable
设置为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 {
...
}
当您需要创建新的子实体时,请使用构造函数,并且由于子实体鉴别器而隐式赋值类型。当您读取现有实体实例并获取关联的分配类型时,将加载相应的分配类型。