Scenario: I have two tables named Document and Element in my postgres db. The Relationship between them is one-to-many(one document has many elements). So the element table has a foreign key of document id. Now I need to have a sequence for the element ids starting from the initial value for each document id.
Eg :
e_id | d_id
------------
1 | x
2 | x
3 | x
1 | y
2 | y
3 | y
Once I have this, There is a next level relationship between element and another table called Labeled Element which has a composite key of Composite Key(document_id, element_id) and labeler_id.
Questions:
How to generate the sequence to start off from the initial value for each document id?
How do I show the JPA Mapping of the composite key in the label table. Specifically which field I map the document id too?
Here are the models for your convenience(In Scala):
Element
@Embeddable
class CompositeKey extends Serializable {
@BeanProperty
@Column(name = "id", nullable = false)
var id : Long = _
@BeanProperty
@Column(name = "document_id", nullable = false)
var documentId : UUID = _
def this(id: Long, documentId: UUID) = {
this()
this.id = id
this.documentId = documentId
}
}
@Entity
@DynamicUpdate
@Table(name = "element")
class Element {
@EmbeddedId
@BeanProperty
@GeneratedValue(strategy = GenerationType.SEQUENCE)
var id: CompositeKey = _
@MapsId("documentId")
@BeanProperty
@JoinColumn(name = "document_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
var document: Document = _
LabeledElement
@Embeddable
class LabeledElementKey extends Serializable {
@BeanProperty
@Column(name = "document_id", nullable = false)
var documentId : UUID = _
@BeanProperty
@Column(name = "labeler_id", nullable = false)
var labelerId : Long = _
@BeanProperty
@Column(name = "element_id", nullable = false)
var elementId : Long = _
def this(documentId : UUID, labelerId : Long, elementId : Long) = {
this()
this.documentId = documentId
this.labelerId = labelerId
this.elementId = elementId
}
}
@Entity
@Table(name = "labeled_element")
class LabeledElement {
@EmbeddedId
@BeanProperty
@Column(unique = true)
var id : LabeledElementKey = _
@MapsId("labelerId")
@JoinColumn(name = "labeler_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
var labeler: Labeler = _
@MapsId("elementId")
@JoinColumns(Array(
new JoinColumn (name = "element_id", referencedColumnName = "id"),
new JoinColumn (name = "document_id", referencedColumnName = "document_id")
))
@ManyToOne(fetch = FetchType.LAZY)
var element: Element = _
// goes on
with the way it is right now, I get the following error :
org.hibernate.AnnotationException: No identifier specified for entity: model.Element
Your Efforts will be appreciated. Thanks!
您的Element类具有注释@Embeddable
而不是@EmbeddedId
。
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.