gpt4 book ai didi

java - 为什么此设置不适用于 JPA 和 Hibernate 中的外键关系?

转载 作者:行者123 更新时间:2023-11-30 05:02:24 26 4
gpt4 key购买 nike

我正在尝试与 JPA 建立外键关系注释并使用 Hibernate 生成表格,但我收到错误并且看不到其原因。我执行 User 中的 main 方法来生成表。

这是错误 -

INFO: Hibernate Validator not found: ignoring
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: com.site.model.Name, at table: USER, for columns: [org.hibernate.mapping.Column(name)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:164)
at org.hibernate.mapping.Column.getSqlType(Column.java:208)
at org.hibernate.mapping.Table.sqlCreateString(Table.java:418)
at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:1099)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:131)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:92)
at com.thepoliticalbottomline.model.User.main(User.java:55)

以及源代码,

package com.sitename.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
public class User {
private Long id;
private String password;
private Name name;

@Id
@GeneratedValue
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public void setName(Name name) {
this.name = name;
}
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Name getName() {
return name;
}

public static void main(String args[]) {
Configuration config = new Configuration();
config.addAnnotatedClass(User.class);
config.addAnnotatedClass(Name.class);
config.configure();
new SchemaExport(config).create(true, true);
}
}

类名,

package com.sitename.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Name {
private Long id;
private String first;
private String middle;
private String last;

/**
* @return the id
*/
@Id
@GeneratedValue
public Long getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}

/**
* @return the first
*/
public String getFirst() {
return first;
}

/**
* @param first the first to set
*/
public void setFirst(String first) {
this.first = first;
}

/**
* @return the middle
*/
public String getMiddle() {
return middle;
}

/**
* @param middle the middle to set
*/
public void setMiddle(String middle) {
this.middle = middle;
}

/**
* @return the last
*/
public String getLast() {
return last;
}

/**
* @param last the last to set
*/
public void setLast(String last) {
this.last = last;
}
}

最佳答案

您不能在属性和方法上混合使用 JPA 注释。您只能注释给定类中的属性方法。

这适用于 hibernate 。我不确定其他 JPA 实现。

关于java - 为什么此设置不适用于 JPA 和 Hibernate 中的外键关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6240069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com