태그 보관물: jakarta-ee

jakarta-ee

엔티티 오류 매핑의 또 다른 반복 열 private Long productId;

다른 모든 게시물에도 불구하고 MacOSX, NetBeans 7.2의 GlassFish에서이 오류에 대한 해결책을 찾을 수 없습니다.

Here the error :
SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer
prepare method
SEVERE: Exception while preparing the app
SEVERE: [PersistenceUnit: supmarket] Unable to build EntityManagerFactory

...

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:
com.supmarket.entity.Sale column: customerId
(should be mapped with insert="false" update="false")

여기에 코드 :

Sale.java

@Entity
public class Sale {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable=false)
    private Long idFromAgency;

    private float amountSold;

    private String agency;

    @Temporal(javax.persistence.TemporalType.DATE)
    private Date createdate;

    @Column(nullable=false)
    private Long productId;

    @Column(nullable=false)
    private Long customerId;

    @ManyToOne(optional=false)
    @JoinColumn(name="productId",referencedColumnName="id_product")
    private Product product;

    @ManyToOne(optional=false)
    @JoinColumn(name="customerId",referencedColumnName="id_customer")
    private Customer customer;


    public void Sale(){}    
    public void Sale(Long idFromAgency, float amountSold, String agency
            , Date createDate, Long productId, Long customerId){        
        ...
    }

    // then getters/setters
}

Customer.java

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id_customer")
    private Long id_customer;

    @Column(nullable=false)
    private Long idFromAgency;

    private String  gender,
                    maritalState,
                    firstname,
                    lastname,
                    incomeLevel;

    @OneToMany(mappedBy="customer",targetEntity=Sale.class, fetch=FetchType.EAGER)
    private Collection sales;


    public void Customer(){}

    public void Customer(Long idFromAgency, String gender, String maritalState,
            String firstname, String lastname, String incomeLevel) {
        ...
    }

}

Product.java

public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id_product")
    private Long id_product;

    @Column(nullable=false)
    private Long idFromAgency;

    private String name;

    @OneToMany(mappedBy="product",targetEntity=Sale.class, fetch=FetchType.EAGER)
    private Collection sales;

    //constructors + getters +setters
}


답변

메시지는 명확합니다. 매핑에 반복되는 열이 있습니다. 즉, 동일한 데이터베이스 열을 두 번 매핑했습니다. 그리고 실제로 :

@Column(nullable=false)
private Long customerId;

그리고 또한:

@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer")
private Customer customer;

(그리고 동일하게 productId/ product).

다른 엔티티를 ID로 참조해서는 안되며 엔티티에 대한 직접 참조로 참조해야합니다. customerId필드를 제거하면 쓸모가 없습니다. 그리고 productId. 판매 고객 ID를 원하는 경우 다음을 수행하면됩니다.

sale.getCustomer().getId()

답변

누군가가 이미 JPA 주석을 배치했지만 관계를 정의하지 않은 레거시 데이터베이스에 갇혀 있고 이제 코드에서 사용하기 위해 정의하려고 시도하는 경우 다른 코드로 인해 customerId @Column을 삭제하지 못할 수 있습니다. 이미 직접 참조 할 수 있습니다. 이 경우 다음과 같이 관계를 정의하십시오.

@ManyToOne(optional=false)
@JoinColumn(name="productId",referencedColumnName="id_product", insertable=false, updatable=false)
private Product product;

@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer", insertable=false, updatable=false)
private Customer customer;

이를 통해 관계에 액세스 할 수 있습니다. 그러나 관계를 추가 / 업데이트하려면 정의 된 @Column 값을 통해 외래 키를 직접 조작해야합니다. 이상적인 상황은 아니지만 이런 상황이 발생하면 최소한 관계를 정의하여 JPQL을 성공적으로 사용할 수 있습니다.


답변

이것을 사용하면 나를 위해 일합니다.

@Column(name = "candidate_id", nullable=false)
private Long candidate_id;
@ManyToOne(optional=false)
@JoinColumn(name = "candidate_id", insertable=false, updatable=false)
private Candidate candidate;

답변

@Id
@Column(name = "COLUMN_NAME", nullable = false)
public Long getId() {
    return id;
}

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = SomeCustomEntity.class)
@JoinColumn(name = "COLUMN_NAME", referencedColumnName = "COLUMN_NAME", nullable = false, updatable = false, insertable = false)
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.ALL)
public List<SomeCustomEntity> getAbschreibareAustattungen() {
    return abschreibareAustattungen;
}

이미 열을 매핑하고 실수 로 @JoinColumn 최대 절전 모드 에서 namereferencedColumnName 에 대해 동일한 값을 설정 하면 동일한 어리석은 오류가 발생합니다

오류:

원인 : org.hibernate.MappingException : 엔티티 매핑에서 반복되는 열 : com.testtest.SomeCustomEntity 열 : COLUMN_NAME (insert = “false”update = “false”로 매핑되어야 함)


답변

이것이 도움이되기를 바랍니다!

@OneToOne(optional = false)
    @JoinColumn(name = "department_id", insertable = false, updatable = false)
    @JsonManagedReference
    private Department department;

@JsonIgnore
    public Department getDepartment() {
        return department;
    }

@OneToOne(mappedBy = "department")
private Designation designation;

@JsonIgnore
    public Designation getDesignation() {
        return designation;
    }

답변

모든 속성에 대해 하나의 setter 및 getter 만 제공하도록주의하십시오. 접근하는 가장 좋은 방법은 모든 속성의 정의를 기록한 다음 수동으로 수행하는 대신 eclipse generate setter 및 getter 유틸리티를 사용하는 것입니다. 이 옵션은 오른쪽 클릭-> 소스-> Getter 및 Setter 생성에 있습니다.


답변

즉, 엔티티 클래스에서 열을 두 번 매핑합니다. 예를 들어 설명 …

    @Column(name = "column1")
    private String object1;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "column1", referencedColumnName = "column1")
    private TableClass object2;

위 코드 스 니펫의 문제는 매핑을 반복한다는 것입니다.

해결책

매핑은 중요한 부분이므로 제거하고 싶지 않습니다. 대신

    @Column(name = "column1")
    private String uniqueId;

TableClass의 개체를 만들고 여기에 Object1의 문자열 값을 할당하여 object1의 값을 전달할 수 있습니다.

이것은 100 % 작동합니다. Postgres 및 Oracle 데이터베이스로 이것을 테스트했습니다.