我有一个带有复合键的实体。 订阅和来源具有多对多关系。
@Entity
@Table(name = "SUBSCRIPTION_SOURCES")
data class SubscriptionSource (
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("subs_id")
val subscription: Subscription,
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("source_id")
val source: Source,
@EmbeddedId
val subscriptionSourceId: SubscriptionSourceId = SubscriptionSourceId(subscription.id, source.id),
@Column(name = "value")
val value : Long
)
Table "SUBSCRIPTION_SOURCES" has only three columns: subs_id, source_id, value.
我正在使用Spring Data存储库来处理它。
当我尝试使用方法 saveAll 时,它会抛出
“ com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列'subscripti0_.subscription_id'”
在保存后尝试返回值。
Hibernate生成以下查询:
select
subscripti0_.source_id as source_i1_6_0_,
subscripti0_.subs_id as subs_id2_6_0_,
subscripti0_.subscription_id as subscrip4_6_0_, // error happens here
subscripti0_.value as value3_6_0_
from
SUBSCRIPTION_SOURCES subscripti0_
where
subscripti0_.source_id=?
and subscripti0_.subs_id=?
所以,现在让我感到困惑的是,如果我的实体中甚至没有此字段,为什么Hibernate会将带有 subscription_id 的字符串放入查询中?
类 SubscriptionSourceId :
@Embeddable
data class SubscriptionSourceId (
@Column(name = "subs_id")
val subsId : Long?,
@Column(name = "source_id")
val sourceId : Long?
) : Serializable
类订阅:
@Entity
@Table(name = "SUBSCRIPTIONS")
data class Subscription(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long? = null,
@Column(name = "subscription_id")
var subscriptionId: String?,
@Column(name = "submitted_on")
var submittedOn: LocalDateTime?,
@Column(name = "status_MPP")
var statusMpp: String?,
@Column(name = "requested_by_user")
var requestedByUser: String?,
@Column(name = "requested_by_user_email")
var requestedByUserEmail: String?,
@Column(name = "organization_name")
var organizationName: String?,
@Column(name = "vm")
var virtualMachine: String?,
@Column(name = "node")
var node: String?,
@Column(name = "period")
var period: String?,
@Column(name = "ad_criterion")
var adCriterion: String?,
@Column(name = "name")
var name: String?,
@Column(name = "description")
var description: String?,
@Column(name = "s_number")
var sNumber: String?,
@Column(name = "p_number")
var pNumber: String?,
@Column(name = "reason")
var reason: String?,
@Column(name = "months")
var months: String?,
@Column(name = "start_date")
var startDate: String?,
@Column(name = "x_start_date")
var actualStartDay: String? = startDate,
@Column(name = "end_date")
var endDate: String?,
@Column(name = "x_end_date")
var actualEndDate: String? = endDate,
@Column(name = "channel")
var channel: String?,
@Column(name = "connection")
var connection: String?,
@Column(name = "connection_data")
var connectionData: String?,
@Column(name = "action")
var action: String?,
@Column(name = "department")
var department: String?,
@Column(name = "login")
var login: String?,
@Column(name = "customer_name")
var customerName: String?,
@Column(name = "customer_postaddress")
var customerPostAddress: String?,
@Column(name = "phone")
var phone: String?,
@Column(name = "customer_chief")
var customerChief: String?,
@Column(name = "user_name")
var userName: String?,
@Column(name = "position")
var position: String?,
@Column(name = "user_postaddress")
var userPostAddress: String?,
@Column(name = "user_phone")
var userPhone: String?,
@Column(name = "user_chief")
var userChief: String?,
@Column(name = "location")
var location: String?,
@Enumerated(EnumType.STRING)
@Column(name = "status")
var status: SubscriptionStatus? = SubscriptionStatus.REQUEST,
@Column(name = "comment")
var comment: String? = "",
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "MTM_NODES2SUBSCRIPTIONS",
joinColumns = [JoinColumn(name = "subs_id")],
inverseJoinColumns = [JoinColumn(name = "node_id")]
)
var nodes : Set<Node> = emptySet(),
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "SUBSCRIPTION_SOURCES",
joinColumns = [JoinColumn(name = "subs_id", referencedColumnName = "id")],
inverseJoinColumns = [JoinColumn(name = "source_id", referencedColumnName = "id")])
val sources : Set<Source?> = emptySet(),
@Column(name = "db_status")
val dbStatus: SubscriptionDbStatus?
)
答案 0 :(得分:0)
终于我解决了我的问题。我已将@JoinColumn批注添加到 SubscriptionSource 类的字段中,并按照JB Nizet的建议更改了@MapsId批注中的值。现在看起来像这样。
@Entity
@Table(name = "SUBSCRIPTION_SOURCES")
data class SubscriptionSource (
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("subsId")
@JoinColumn(name = "subs_id")
val subscription: Subscription,
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("sourceId")
@JoinColumn(name = "source_id")
val source: Source,
@EmbeddedId
val subscriptionSourceId: SubscriptionSourceId = SubscriptionSourceId(subscription.id, source.id),
@Column(name = "value")
val value : Long
)
感谢所有人。