I have two tables in a MySQL Database. One is named documento_tributario_electronico and the other is named detalle.
The documento_Tributario_electronico table is:
CREATE TABLE `documento_tributario_electronico` (
`tipo_documento` INT(11) NOT NULL,
`numero` BIGINT(20) NOT NULL,
`fecha_emision` DATE NOT NULL DEFAULT 'curdate()',
`indicador_no_rebaja` INT(11) NULL DEFAULT NULL,
`tipo_despacho` INT(11) NULL DEFAULT NULL,
`indicador_traslado` INT(11) NULL DEFAULT NULL,
`tipo_impresion` VARCHAR(1) NOT NULL DEFAULT 'N' COLLATE 'utf8mb4_0900_ai_ci',
`indicador_servicio` INT(11) NULL DEFAULT NULL,
`indicador_montos_brutos` INT(11) NULL DEFAULT NULL,
`tipo_transaccion_compra` INT(11) NULL DEFAULT NULL,
`tipo_transaccion_venta` INT(11) NOT NULL DEFAULT '1',
`forma_pago` INT(11) NOT NULL DEFAULT '2',
`fecha_cancelacion` DATE NULL DEFAULT NULL,
`monto_cancelado` BIGINT(20) NULL DEFAULT NULL,
`saldo_insoluto` BIGINT(20) NULL DEFAULT NULL,
`periodo_desde` DATE NULL DEFAULT NULL,
`periodo_hasta` DATE NULL DEFAULT NULL,
`medio_pago` VARCHAR(2) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`tipo_cuenta_pago` VARCHAR(2) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`cuenta_pago` VARCHAR(20) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`banco_pago` VARCHAR(40) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`terminos_pago_codigo` VARCHAR(4) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`terminos_pago_glosa` VARCHAR(100) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`terminos_pago_dias` INT(11) NULL DEFAULT NULL,
`fecha_vencimiento` DATE NULL DEFAULT NULL,
`tipo_factura_especial` INT(11) NULL DEFAULT NULL,
`rut_empresa` VARCHAR(10) NOT NULL COLLATE 'utf8mb4_0900_ai_ci',
`rut_mandante` VARCHAR(10) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`rut_cliente` VARCHAR(10) NOT NULL COLLATE 'utf8mb4_0900_ai_ci',
`rut_solicitante` VARCHAR(10) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`patente` VARCHAR(8) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`rut_transportista` VARCHAR(10) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`rut_chofer` VARCHAR(10) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`nombre_chofer` VARCHAR(30) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`direccion_destino` VARCHAR(70) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`comuna_destino` INT(11) NULL DEFAULT NULL,
`ciudad_destino` INT(11) NULL DEFAULT NULL,
`monto_neto` BIGINT(20) NULL DEFAULT NULL,
`monto_exento` BIGINT(20) NULL DEFAULT NULL,
`monto_base_faenamiento_carne` BIGINT(20) NULL DEFAULT NULL,
`monto_base_margen_comercializacion` BIGINT(20) NULL DEFAULT NULL,
`tasa_iva` FLOAT NULL DEFAULT '9.99',
`iva` BIGINT(20) NULL DEFAULT NULL,
`iva_propio` BIGINT(20) NULL DEFAULT NULL,
`iva_terceros` BIGINT(20) NULL DEFAULT NULL,
`iva_no_retenido` BIGINT(20) NULL DEFAULT NULL,
`aplica_credito_especial_empresas_constructoras` TINYINT(4) NOT NULL DEFAULT '0',
`monto_total` BIGINT(20) NOT NULL,
`tipo_otra_moneda` VARCHAR(15) NULL DEFAULT '' COLLATE 'utf8mb4_0900_ai_ci',
`factor_conversion_otra_moneda` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`tipo_documento`, `numero`, `rut_empresa`) USING BTREE
)
The detalle table is:
CREATE TABLE `detalle` (
`rut_empresa` VARCHAR(10) NOT NULL COLLATE 'utf8mb4_0900_ai_ci',
`tipo_documento` INT(11) NOT NULL,
`numero` BIGINT(20) NOT NULL,
`numero_detalle` INT(11) NOT NULL,
`agenteRetenedor` INT(11) NOT NULL DEFAULT '0',
`monto_base_faenamiento_carne` BIGINT(20) NULL DEFAULT NULL,
`monto_base_margen_comercializacion` BIGINT(20) NULL DEFAULT NULL,
`precio_unitario_neto_consumidor_final` BIGINT(20) NULL DEFAULT NULL,
`codigo_producto` INT(11) NOT NULL,
`descripcion_adicional` VARCHAR(1000) NULL DEFAULT NULL COLLATE 'utf8mb4_0900_ai_ci',
`cantidad_referencia` FLOAT NULL DEFAULT NULL,
`cantidad` FLOAT NOT NULL,
`fecha_elaboracion` DATE NULL DEFAULT NULL,
`fecha_vencimiento` DATE NULL DEFAULT NULL,
`descuento_pct` FLOAT NULL DEFAULT NULL,
`descuento_monto` BIGINT(20) NULL DEFAULT NULL,
`recargo_pct` FLOAT NULL DEFAULT NULL,
`recargo_monto` BIGINT(20) NULL DEFAULT NULL,
`codigo_impuesto_retencion1` INT(11) NULL DEFAULT NULL,
`codigo_impuesto_retencion2` INT(11) NULL DEFAULT NULL,
`monto_item` BIGINT(20) NULL DEFAULT NULL,
PRIMARY KEY (`rut_empresa`, `tipo_documento`, `numero`, `numero_detalle`) USING BTREE
)
Also, I'm developing a Spring Boot Application with Spring JPA and Lombok. Due to the fact that both tables have composite primary keys, I had to create a Embeddable class for each entity's Id. Also, there is a relationship one to many from documento_tributario_electronico to detalle.
I implemented the entities for both tables as follows:
The DocumentoTributarioElectronico entity:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name="documento_tributario_electronico")
public class DocumentoTributarioElectronico implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private DocumentoTributarioElectronicoPK id;
@ManyToOne(fetch=FetchType.LAZY)
@MapsId("rutEmpresa")
private Empresa emisor;
@OneToOne(fetch=FetchType.LAZY)
@MapsId("tipoDocumento")
private TipoDocumento tipoDocumento;
@Column(name="fecha_emision", nullable=false)
@Temporal(TemporalType.DATE)
private Date fechaEmision;
@Column(name="indicador_no_rebaja", nullable=true)
private Integer indicadorNoRebaja;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tipo_despacho", referencedColumnName="codigo", nullable=true)
private TipoDespacho tipoDespacho;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="indicador_traslado", referencedColumnName="codigo", nullable=true)
private IndicadorTraslado indicadorTraslado;
@Column(name="tipo_impresion", length=1, columnDefinition="VARCHAR(1) DEFAULT 'N'")
private String tipoImpresion;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="indicador_servicio", referencedColumnName="codigo", nullable=true)
private IndicadorServicio indicadorServicio;
@Column(name="indicador_montos_brutos", nullable=true)
private Integer indicadorMontosBrutos;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tipo_transaccion_compra", referencedColumnName="codigo", nullable=true)
private TipoTransaccionCompra tipoTransaccionCompra;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tipo_transaccion_venta", referencedColumnName="codigo", columnDefinition="INT(11) DEFAULT 1")
private TipoTransaccionVenta tipoTransaccionVenta;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="forma_pago", referencedColumnName="codigo", columnDefinition="INT(11) DEFAULT 2")
private FormaPago formaPago;
@Column(name="fecha_cancelacion", nullable=true)
@Temporal(TemporalType.DATE)
private Date fechaCancelacion;
@Column(name="monto_cancelado", nullable=true)
private Long montoCancelado;
@Column(name="saldo_insoluto", nullable=true)
private Long saldoInsoluto;
@OneToMany(fetch=FetchType.LAZY, mappedBy="documentoTributarioElectronico")
private List<MontoPago> montosPago;
@Column(name="periodo_desde", nullable=true)
@Temporal(TemporalType.DATE)
private Date periodoDesde;
@Column(name="periodo_hasta", nullable=true)
@Temporal(TemporalType.DATE)
private Date periodoHasta;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="medio_pago", referencedColumnName="codigo", nullable=true)
private MedioPago medioPago;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tipo_cuenta_pago", referencedColumnName="codigo", nullable=true)
private TipoCuentaPago tipoCuentaPago;
@Column(name="cuenta_pago", length=20, nullable=true)
private String cuentaPago;
@Column(name="banco_pago", length=40, nullable=true)
private String bancoPago;
@Column(name="terminos_pago_codigo", length=4, nullable=true)
private String terminosPagoCodigo;
@Column(name="terminos_pago_glosa", length=100, nullable=true)
private String terminosPagoGlosa;
@Column(name="terminos_pago_dias", nullable=true)
private Integer terminosPagoDias;
@Column(name="fecha_vencimiento", nullable=true)
@Temporal(TemporalType.DATE)
private Date fechaVencimiento;
@Column(name="rut_mandante", length=10, nullable=true)
private String rutMandante;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="rut_cliente", referencedColumnName="rut", nullable=false)
private Cliente cliente;
@Column(name="rut_solicitante", length=10, nullable=true)
private String rutSolicitante;
@Column(name="patente", length=8, nullable=true)
private String patente;
@Column(name="rut_transportista", length=10, nullable=true)
private String rutTransportista;
@Column(name="rut_chofer", length=10, nullable=true)
private String rutChofer;
@Column(name="nombre_chofer", length=30, nullable=true)
private String nombreChofer;
@Column(name="direccion_destino", length=70, nullable=true)
private String direccionPostal;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ciudad_destino", referencedColumnName="codigo", nullable=true)
private Ciudad ciudadPostal;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="comuna_destino", referencedColumnName="codigo", nullable=true)
private Comuna comunaPostal;
@Column(name="monto_neto", nullable=true)
private Long montoNeto;
@Column(name="monto_exento", nullable=true)
private Long montoExento;
@Column(name="monto_base_faenamiento_carne", nullable=true)
private Long montoBaseFaenamientoCarne;
@Column(name="monto_base_margen_comercializacion", nullable=true)
private Long montoBaseMargenComercializacion;
@Column(name="tasa_iva", nullable=true)
private Float tasaIva;
@Column(name="iva", nullable=true)
private Long iva;
@Column(name="iva_propio", nullable=true)
private Long ivaPropio;
@Column(name="iva_terceros", nullable=true)
private Long ivaTerceros;
@Column(name="iva_no_retenido", nullable=true)
private Long ivaNoRetenido;
@Column(name="aplica_credito_especial_empresas_constructoras", columnDefinition="TINYINT(1) DEFAULT 0")
private Boolean aplicaCreditoEspecialEmpresasConstructoras;
@Column(name="monto_total", nullable=false)
private Long montoTotal;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="tipo_otra_moneda", referencedColumnName="codigo", nullable=true)
private Moneda tipoOtraMoneda;
@Column(name="factor_conversion_otra_moneda", nullable=true)
private Float factorConversionOtraMoneda;
@OneToMany(fetch=FetchType.LAZY, mappedBy="dte")
private List<Detalle> detalles;
@OneToMany(fetch=FetchType.LAZY, mappedBy="dte")
private List<SubtotalInformativo> subtotales;
@OneToMany(fetch=FetchType.LAZY, mappedBy="dte")
private List<DescuentoRecargoGlobal> descuentosRecargosGlobales;
@OneToMany(fetch=FetchType.LAZY, mappedBy="dte")
private List<Referencia> referencias;
}
The Detalle entity:
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name="detalle")
public class Detalle implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private DetallePK id;
@ManyToOne(fetch=FetchType.LAZY)
@MapsId("dteId")
private DocumentoTributarioElectronico dte;
@OneToMany(fetch=FetchType.LAZY, mappedBy="detalle")
private List<Subcantidad> subcantidades;
@OneToMany(fetch=FetchType.LAZY, mappedBy="detalle")
private List<DetalleOtraMoneda> detalleOtraMoneda;
@OneToMany(fetch=FetchType.LAZY, mappedBy="detalle")
private List<DistribucionDescuento> distribucionDescuento;
@OneToMany(fetch=FetchType.LAZY, mappedBy="detalle")
private List<DistribucionRecargo> distribucionRecargo;
@Column(name="agente_retenedor", columnDefinition="INT(11) DEFAULT 0")
private Integer agenteRetenedor;
@Column(name="monto_base_faenamiento_carne", nullable=true)
private Long montoBaseFaenamientoCarne;
@Column(name="monto_base_margen_comercializacion", nullable=true)
private Long montoBaseMargenComercializacion;
@Column(name="precio_unitario_neto_consumidor_final", nullable=true)
private Long precioUnitarioNetoConsumidorFinal;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="rut_empresa", referencedColumnName="rut_empresa")
@JoinColumn(name="codigo_producto", referencedColumnName="codigo")
private Producto producto;
@Column(name="descripcion_adicional", length=1000, nullable=true)
private String descripcionAdicional;
@Column(name="cantidad_referencia", nullable=true)
private Float cantidadReferencia;
@Column(name="cantidad", nullable=false)
private Float cantidad;
@Column(name="fecha_elaboracion", nullable=true)
@Temporal(TemporalType.DATE)
private Date fechaElaboracion;
@Column(name="fecha_vencimiento", nullable=true)
@Temporal(TemporalType.DATE)
private Date fechaVencimiento;
@Column(name="descuento_pct", nullable=true)
private Float descuentoPct;
@Column(name="descuento_monto", nullable=true)
private Long descuentoMonto;
@Column(name="recargo_pct", nullable=true)
private Float recargoPct;
@Column(name="recargo_monto", nullable=true)
private Long recargoMonto;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="codigo_impuesto_retencion1", referencedColumnName="codigo", nullable=true)
ImpuestoRetencion impuestoRetencion1;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="codigo_impuesto_retencion2", referencedColumnName="codigo", nullable=true)
ImpuestoRetencion impuestoRetencion2;
@Column(name="monto_item", nullable=false)
private Long montoItem;
@ManyToMany(fetch=FetchType.LAZY, mappedBy="detallesPorSubtotal")
private List<SubtotalInformativo> subtotales;
}
Now notice how are defined the Embeddable classes for each entity's id:
The DocumentoTributarioElectronicoPK class:
@Embeddable
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class DocumentoTributarioElectronicoPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="rut_empresa", length=10, nullable=false)
private String rutEmpresa;
@Column(name="tipo_documento", nullable=false)
private Integer tipoDocumento;
@Column(name="numero", nullable=false)
private Long numero;
}
The DetallePK class
@Embeddable
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class DetallePK implements Serializable {
private static final long serialVersionUID = 1L;
@Embedded
private DocumentoTributarioElectronicoPK dteId;
@Column(name="numero_detalle", nullable=false)
private Integer numeroDetalle;
}
When I run the application, I get this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to find column with logical name tipo_documento in table detalle
But the column tipo_documento IS in detalle!
I'd wish to know what is wrong with both entities.
Thanks in advance
EDIT: Please consider the fact that the DetallePK class is also embedded in other entities that are pointing to tables with a relationship many to one from each one of those tables to detalle.
EDIT: As requested, here is the current occurrences of referencedColumnName="tipo_documento". These are in another entity class
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "detalles_subtotal", inverseJoinColumns = {
@JoinColumn(name = "rut_empresa", referencedColumnName = "rut_empresa"),
@JoinColumn(name = "tipo_documento", referencedColumnName = "tipo_documento"),
@JoinColumn(name = "numero", referencedColumnName = "numero"),
@JoinColumn(name = "numero_detalle", referencedColumnName = "numero_detalle") }, joinColumns = {
@JoinColumn(name = "rut_empresa", referencedColumnName = "rut_empresa"),
@JoinColumn(name = "tipo_documento", referencedColumnName = "tipo_documento"),
@JoinColumn(name = "numero", referencedColumnName = "numero"),
@JoinColumn(name = "numero_subtotal", referencedColumnName = "numero_subtotal") })
private List<Detalle> detallesPorSubtotal;
EDIT: As per requested, I followed the accepted answer for the question in this link: https://stackoverflow.com/a/31389777/1429387
This gives me another error:
org.hibernate.MappingException: Repeated column in mapping for entity: cl.maraneda.neopos.entities.Detalle column: rut_empresa (should be mapped with insert="false" update="false")
Obviously, map rut_empresa as non insertable is not an option