持久保存后,相同类型的多个@Embedded字段始终为空

时间:2018-08-02 15:23:20

标签: java spring inheritance spring-data-jpa embeddable

我实现了以下层次结构:

Abstract Superclass: ConnectionTechnologyDetails
Subclass: AS2
Embeddable: AS2Details

与父类相比,子类AS2具有其他属性和方法。其他属性是AS2Details的@Embedded类型。

将AS2子类实例保存到其存储库时,@ Embedded字段似乎丢失了。当我对保存的实例进行优先处理时,所有@Embedded字段均为 null 。我不知道为什么,因为在保存到资源库之前进行打印会导致填充的@Embedded字段。

坚持后的结果

  "connectionTechnologyDetails": {
        "id": 2,
        "connectionTechnologyName": "AS2",
        "senderReceiverIds": {
            "senderIDOutbound": "HLCU",
            "receiverIDOutbound": "BASF",
            "senderIdInbound": "BASF",
            "receiverIdInbound": "HLCU"
        },
        "companyTestDetails": null,
        "companyProdDetails": null,
        "hlagTestDetails": null,
        "hlagProdDetails": null,
        "testString": "testStringShowsUp"
    }

ConnectionTechnologyDetails

import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "TECH_TYPE")
public abstract class ConnectionTechnologyDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne
@PrimaryKeyJoinColumn
private Connection connection;

@NotNull
private ConnectionTechnologyName connectionTechnologyName;

@Embedded
private SenderReceiverIds senderReceiverIds;

public ConnectionTechnologyDetails(ConnectionTechnologyName connectionTechnologyName, SenderReceiverIds senderReceiverIds) {
    this.connectionTechnologyName = connectionTechnologyName;
    this.senderReceiverIds = senderReceiverIds;
}

public ConnectionTechnologyDetails() {

}

public ConnectionTechnologyName getConnectionTechnologyName() {
    return connectionTechnologyName;
}

public void setConnectionTechnologyName(ConnectionTechnologyName connectionTechnologyName) {
    this.connectionTechnologyName = connectionTechnologyName;
}

public SenderReceiverIds getSenderReceiverIds() {
    return senderReceiverIds;
}

public void setSenderReceiverIds(SenderReceiverIds senderReceiverIds) {
    this.senderReceiverIds = senderReceiverIds;
}

public Long getId() {
    return id;
}

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

public enum ConnectionTechnologyName {
    SMTP,
    AS2,
    UNSECURE_FTP,
    SECURE_FTP,
    FTP_SECURE
}

@Override
public String toString() {
    return "ConnectionTechnologyDetails: Name=" +getConnectionTechnologyName().name()
            + " SenderReceiverIds=" + getSenderReceiverIds();
}

}

AS2

(甚至尝试对第一个@Embedded字段进行硬编码。但是,即使这样也不起作用。)

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity(name = "AS2")
@Table(name = "AS2")
@DiscriminatorValue("AS2")
public class AS2 extends ConnectionTechnologyDetails {
    @NotNull
    @Embedded
    private AS2Details companyTestDetails = new AS2Details("192.1.168.1",
            "ctest", "localhost:8080", "ctest2", "90", "111.00.5.4",
            "scharpe", "+9+992++*9*+", "AS3");
    @NotNull
    @Embedded
    private AS2Details companyProdDetails;
    @NotNull
    @Embedded
    private AS2Details hlagTestDetails;
    @NotNull
    @Embedded
    private AS2Details hlagProdDetails;
    private String testString;

    public AS2(ConnectionTechnologyName connectionTechnologyName, SenderReceiverIds senderReceiverIds,
               AS2Details companyTestDetails, AS2Details companyProdDetails, AS2Details hlagTestDetails,
               AS2Details hlagProdDetails) {
        super(connectionTechnologyName, senderReceiverIds);
        System.err.println("ERROR---- companyTestDetails = " + companyTestDetails);
        this.companyTestDetails = companyTestDetails;
        this.companyProdDetails = companyProdDetails;
        this.hlagTestDetails = hlagTestDetails;
        this.hlagProdDetails = hlagProdDetails;
        testString = "testStringShowsUp";
    }

    public AS2() {
        super();
    }


    public void setHlagProdDetails(AS2Details hlagProdDetails) {
        this.hlagProdDetails = hlagProdDetails;
    }

    @Override
    public String toString() {
        return "AS2 Technology: Company: " + getCompanyTestDetails() + " / " + getCompanyProdDetails() + " HLAG: " +
                getHlagTestDetails() + " " + getHlagTestDetails() + getTestString();
    }

    public AS2Details getCompanyTestDetails() {
        return companyTestDetails;
    }

    public void setCompanyTestDetails(AS2Details companyTestDetails) {
        this.companyTestDetails = companyTestDetails;
    }

    public AS2Details getCompanyProdDetails() {
        return companyProdDetails;
    }

    public void setCompanyProdDetails(AS2Details companyProdDetails) {
        this.companyProdDetails = companyProdDetails;
    }

    public AS2Details getHlagTestDetails() {
        return hlagTestDetails;
    }

    public void setHlagTestDetails(AS2Details hlagTestDetails) {
        this.hlagTestDetails = hlagTestDetails;
    }

    public AS2Details getHlagProdDetails() {
        return hlagProdDetails;
    }

    public String getTestString() {
        return testString;
    }

    public void setTestString(String testString) {
        this.testString = testString;
    }
}

AS2Details

import javax.persistence.Column;
import javax.persistence.Embeddable;

public class AS2Details {
    @Column(name = "ip1", insertable = false, updatable = false)
    private String ip1;
    @Column(name = "as1id1", insertable = false, updatable = false)
    private String AS2ID1;
    @Column(name = "url", insertable = false, updatable = false)
    private String url;
    @Column(name = "as2id2", insertable = false, updatable = false)
    private String AS2ID2;
    @Column(name = "port", insertable = false, updatable = false)
    private String port;
    @Column(name = "ip2", insertable = false, updatable = false)
    private String ip2;
    @Column(name = "as2sofware", insertable = false, updatable = false)
    private String AS2Software;
    @Column(name = "userid", insertable = false, updatable = false)
    private String userId;
    @Column(name = "password", insertable = false, updatable = false)
    private String password;

    public AS2Details(String ip1, String AS2ID1, String url, String AS2ID2,
                      String port, String ip2, String userId, String password, String AS2Software) {
        this.ip1 = ip1;
        this.AS2ID1 = AS2ID1;
        this.url = url;
        this.AS2ID2 = AS2ID2;
        this.port = port;
        this.ip2 = ip2;
        this.userId = userId;
        this.password = password;
        this.AS2Software = AS2Software;
    }

    public AS2Details() {

    }

    public String getIp1() {
        return ip1;
    }

    public void setIp1(String ip1) {
        this.ip1 = ip1;
    }

    public String getAS2ID1() {
        return AS2ID1;
    }

    public void setAS2ID1(String AS2ID1) {
        this.AS2ID1 = AS2ID1;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAS2ID2() {
        return AS2ID2;
    }

    public void setAS2ID2(String AS2ID2) {
        this.AS2ID2 = AS2ID2;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getIp2() {
        return ip2;
    }

    public void setIp2(String ip2) {
        this.ip2 = ip2;
    }

    public String getAS2Software() {
        return AS2Software;
    }

    public void setAS2Software(String AS2Software) {
        this.AS2Software = AS2Software;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "AS2 Details: " + getAS2ID1() + getAS2ID2() + getIp1() + getIp2() + getPassword();
    }
}

在CommandLineRunner中创建数据

    AS2Details companyTestDetails = new AS2Details("192.1.168.1",
            "ctest", "localhost:8080", "ctest2", "90", "111.00.5.4",
            "scharpe", "+9+992++*9*+", "AS3");
    AS2Details companyProdDetails = new AS2Details("192.1.168.1",
            "cp", "localhost:8080", "cp", "90", "111.00.5.4",
            "scharpe", "+9+992++*9*+", "AS3");
    AS2Details hlagTestDetails = new AS2Details("192.1.168.1",
            "htest1", "localhost:8080", "htest2", "90", "111.00.5.4",
            "scharpe", "+9+992++*9*+", "AS3");
    AS2Details hlagProdDetails = new AS2Details("192.1.168.1",
            "hp1", "localhost:8080", "hp2", "90", "111.00.5.4",
            "scharpe", "+9+992++*9*+", "AS3");

    AS2 as2 = connectionTechnologyDetailsRepository.save(new AS2(ConnectionTechnologyDetails
            .ConnectionTechnologyName.AS2, senderReceiverIds,
            companyTestDetails, companyProdDetails, hlagTestDetails, new AS2Details()));

    logger.info("CommandLineRunner: connectionTechnology AS2: " + as2.toString());
    Connection userAs2Connection = new Connection(userRepository.findByUsername("userman").get(),
            contactDetailsRepository.findFirstByOrderByIdAsc(),
            BusinessCase.CONSIGNEE,
            as2);
    connectionRepository.save(userAs2Connection);

为什么嵌入的字段为null,为什么超类的字段被填充? 当我从子类的构造函数中打印字段时,数据正确存在。但这并没有从构造函数中保存到嵌入字段中。

1 个答案:

答案 0 :(得分:0)

自己解决。

如果您映射多个相同类型的@Embedded字段 (AS2Details),则必须覆盖它们的属性。否则,您的表中将发生名称冲突,因为多个列将具有相同的名称。例如,在这种情况下,您将在同一张单个表中包含列URL和列URL。

因此,我向AS2类中的每个嵌入式字段添加了onverride注释。 例如第一个字段:

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="ip1", column=@Column(name="companyTestDetails_ip1")),
        @AttributeOverride(name="AS2ID1", column=@Column(name="companyTestDetails_as2id1")),
        @AttributeOverride(name="url", column=@Column(name="companyTestDetails_url")),
        @AttributeOverride(name="AS2ID2", column=@Column(name="companyTestDetails_as2id2")),
        @AttributeOverride(name="port", column=@Column(name="companyTestDetails_port")),
        @AttributeOverride(name="ip2", column=@Column(name="companyTestDetails_ip2")),
        @AttributeOverride(name="userId", column=@Column(name="companyTestDetails_userId")),
        @AttributeOverride(name="password", column=@Column(name="companyTestDetails_password")),
        @AttributeOverride(name="AS2Software", column=@Column(name="companyTestDetails_as2software"))
})
private AS2Details companyTestDetails;