Java 不使用外键但使用共享字段的休眠表关系

Java 不使用外键但使用共享字段的休眠表关系,java,hibernate,jpa,foreign-keys,Java,Hibernate,Jpa,Foreign Keys,我有几个需要关联的表格存在问题。这些表没有外键,但它们共享一个公共字段。由于ETL过程,我不允许操作这些表。 我有两个实体:成本中心和井眼完井。它们都共享一个名为costCenterNumber的字段。我想使用costCenterNumber进行查询,以便返回这两个实体 CostCenter.java @Entity @XmlRootElement @Table(name = "DIM1_COST_CENTER", schema = "BIDW.dbo") public class CostC

我有几个需要关联的表格存在问题。这些表没有外键,但它们共享一个公共字段。由于ETL过程,我不允许操作这些表。 我有两个实体:成本中心和井眼完井。它们都共享一个名为costCenterNumber的字段。我想使用costCenterNumber进行查询,以便返回这两个实体

CostCenter.java

@Entity
@XmlRootElement

@Table(name = "DIM1_COST_CENTER", schema = "BIDW.dbo")
public class CostCenter implements Serializable {

@Id
@Column(name = "DK_COST_CENTER")
@GeneratedValue(generator="seq_item", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name="seq_item",sequenceName="DK_COST_CENTER", allocationSize=1)
@XmlAttribute
private Integer  id;


@Column(name = "COST_CENTER_NUMBER")
@XmlAttribute
private String costCenterNumber;


@OneToOne (mappedBy = "costCenterNumber", fetch = FetchType.EAGER)
@JoinColumn(name="COST_CENTER_NUMBER")
@XmlAttribute
private WellboreCompletion wellboreCompletion;

public String getCostCenterNumber() {
    return costCenterNumber;
}

public void setCostCenterNumber(String costCenterNumber) {
    this.costCenterNumber = costCenterNumber;
}

    public WellboreCompletion getWellboreCompletion() {
    return wellboreCompletion;
}

public void setWellboreCompletion(WellboreCompletion wellboreCompletion) {
    this.wellboreCompletion = wellboreCompletion;
}

//..more fields and more getters and setters
WellboreCompletion.java

@Entity
@XmlRootElement
@Table(name = "DIM1_WELLBORE_COMPLETION", schema = "BIDW.dbo")
public class WellboreCompletion implements Serializable{

@Id
@Column(name = "DK_WELLBORE_COMPLETION")
@GeneratedValue(generator="seq_item", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name="seq_item",sequenceName="DK_COMPLETION_NAME", allocationSize=1)
@XmlAttribute
private Integer  id;

@OneToOne(targetEntity = CostCenter.class)
@JoinColumn(name = "COST_CENTER_NUMBER",referencedColumnName = "COST_CENTER_NUMBER")
private String costCenterNumber;



public String getCostCenterNumber() {
    return costCenterNumber;
}

public void setCostCenterNumber(String costCenterNumber) {
    this.costCenterNumber = costCenterNumber;
}

.
.
//more fields with getters and setters
端点休息:

 @GET
 @Path("/costcenter/{id}")
 public CostCenter getCostCenter(@PathParam("id") int id) {
    return wellSvc.getCostCenter(id);
 }
我服务的方法:

@Override
public CostCenter getCostCenter(int id) {
    CostCenter cc = BIDWEntityManager.find(CostCenter.class, id);
    return  cc;
}
我知道此搜索使用的是主键ID而不是costCenterNumber,但我认为这将是第一步

日志:

错误:

11:36:04,687 DEBUG [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-/0.0.0.0:8080-1) could not load an entity: [com.dvn.search.landFiles.detail.entity.CostCenter#44462] [select costcenter0_.DK_COST_CENTER as DK_COST_1_1_1_, costcenter0_.COMPANY_LONG_NAME as COMPANY_2_1_1_, costcenter0_.COST_CENTER_CATEGORY_LONG_NAME as COST_CEN3_1_1_, costcenter0_.COST_CENTER_LONG_NAME as COST_CEN4_1_1_, costcenter0_.COST_CENTER_NUMBER as COST_CEN5_1_1_, costcenter0_.COUNTRY_LONG_NAME as COUNTRY_6_1_1_, costcenter0_.DISTRICT_LONG_NAME as DISTRICT7_1_1_, costcenter0_.HIER_FIELD_LONG_NAME as HIER_FIE8_1_1_, costcenter0_.HIER_FIELD_CODE as HIER_FIE9_1_1_, costcenter0_.JOINT_VENTURE_CODE as JOINT_V10_1_1_, costcenter0_.JOINT_VENTURE_LONG_NAME as JOINT_V11_1_1_, costcenter0_.OPERATOR_LONG_NAME as OPERATO12_1_1_, costcenter0_.REGION_LONG_NAME as REGION_13_1_1_, wellboreco1_.DK_WELLBORE_COMPLETION as DK_WELLB1_4_0_, wellboreco1_.WB_API_NUMBER as WB_API_N2_4_0_, wellboreco1_.TX_RRC_DISTRICT_ID as TX_RRC_D3_4_0_, wellboreco1_.COMPLETION_DATE as COMPLETI4_4_0_, wellboreco1_.COST_CENTER_NUMBER as COST_CE11_4_0_, wellboreco1_.INITIAL_PRODUCTION_DATE as INITIAL_5_4_0_, wellboreco1_.WB_SURVEY as WB_SURVE6_4_0_, wellboreco1_.WB_GEOGRAPHIC_FOOTAGE as WB_GEOGR7_4_0_, wellboreco1_.CLASS_LONG_NAME as CLASS_LO8_4_0_, wellboreco1_.WELLBORE_NUMBER as WELLBORE9_4_0_, wellboreco1_.STATUS_LONG_NAME as STATUS_10_4_0_ from BIDW.dbo.DIM1_COST_CENTER costcenter0_ left outer join BIDW.dbo.DIM1_WELLBORE_COMPLETION wellboreco1_ on costcenter0_.DK_COST_CENTER=wellboreco1_.COST_CENTER_NUMBER where costcenter0_.DK_COST_CENTER=?]: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'N/A' to data type int.
.
.
WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-/0.0.0.0:8080-1) SQL Error: 245, SQLState: S0001
ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-/0.0.0.0:8080-1) Conversion failed when converting the varchar value 'N/A' to data type int.
INFO  [org.hibernate.event.internal.DefaultLoadEventListener] (http-/0.0.0.0:8080-1) HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not load an entity: [com.dvn.search.landFiles.detail.entity.CostCenter#44462]
DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] (http-/0.0.0.0:8080-1) Mark transaction for rollback
TRACE [org.hibernate.internal.SessionImpl] (http-/0.0.0.0:8080-1) Setting cache mode to: NORMAL
TRACE [org.hibernate.internal.SessionImpl] (http-/0.0.0.0:8080-1) Closing session
TRACE [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl] (http-/0.0.0.0:8080-1) Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@7f22462b]
TRACE [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-/0.0.0.0:8080-1) Closing logical connection
TRACE [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-/0.0.0.0:8080-1) Logical connection closed
TRACE [org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization] (http-/0.0.0.0:8080-1) JTA sync : afterCompletion(4)
TRACE [org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl] (http-/0.0.0.0:8080-1) Transaction afterCompletion callback [status=4]
TRACE [org.hibernate.ejb.AbstractEntityManagerImpl] (http-/0.0.0.0:8080-1) Session was closed; nothing to do
TRACE [org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl] (http-/0.0.0.0:8080-1) after transaction completion
DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-/0.0.0.0:8080-1) Aggressively releasing JDBC connection
TRACE [org.hibernate.internal.SessionImpl] (http-/0.0.0.0:8080-1) after transaction completion
ERROR [org.jboss.as.ejb3.invocation] (http-/0.0.0.0:8080-1) JBAS014134: EJB Invocation failed on component WellServiceImpl for method public abstract com.dvn.search.landFiles.detail.entity.CostCenter com.dvn.search.service.WellService.getCostCenter(int): javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.dvn.search.landFiles.detail.entity.CostCenter#44462]
根本原因似乎是:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'N/A' to data type int.
我不知道为什么会发生这种转换,因为我没有int数据类型。”N/A'是我测试的案例的costCenterNumber的值,但它在实体中声明为字符串,在表中声明为VARCHAR

DDL:

DIM1成本中心:

COST_CENTER_NUMBER VARCHAR(30)
DIM1井眼完井:

COST_CENTER_NUMBER VARCHAR(30),

该sql联接正在比较这两个值:

left outer join BIDW.dbo.DIM1_WELLBORE_COMPLETION wellboreco1_ 
 on costcenter0_.DK_COST_CENTER=wellboreco1_.COST_CENTER_NUMBER 
DK_COST_CENTER是CostCenter对象的整数ID成员。
COST\u CENTER\u NUMBER是井眼对象的字符串成员。

您的COST\u CENTER\u NUMBER定义为VARCHAR,但您的类CostCenter类的id为Int,因此您将获得异常

将ID类型更改为String,它应该可以工作


问题与缺少主键无关。

谢谢您的回复。比较两个字段(其中没有PK或FK)的正确注释是什么?感谢您的回复。比较没有PK或FK的两个字段的正确注释是什么?
left outer join BIDW.dbo.DIM1_WELLBORE_COMPLETION wellboreco1_ 
 on costcenter0_.DK_COST_CENTER=wellboreco1_.COST_CENTER_NUMBER