엔티티 오류 매핑의 또 다른 반복 열
다른 모든 게시물에도 불구하고 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()
If you are stuck with a legacy database where someone already placed JPA annotations on but did NOT define the relationships and you are now trying to define them for use in your code, then you might NOT be able to delete the customerId @Column since other code may directly reference it already. In that case, define the relationships as follows:
@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;
This allows you to access the relationships. However, to add/update to the relationships you will have manipulate the foreign keys directly via their defined @Column values. It's not an ideal situation, but if you are handed this sort of situation, at least you can define the relationships so that you can use JPQL successfully.
use this, is work for me:
@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;
}
If you have already mapped a column and have accidentaly set the same values for name and referencedColumnName in @JoinColumn hibernate gives the same stupid error
Error:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.testtest.SomeCustomEntity column: COLUMN_NAME (should be mapped with insert="false" update="false")
Take care to provide only 1 setter and getter for any attribute. The best way to approach is to write down the definition of all the attributes then use eclipse generate setter and getter utility rather than doing it manually. The option comes on right click-> source -> Generate Getter and Setter.
참고URL : https://stackoverflow.com/questions/15076463/another-repeated-column-in-mapping-for-entity-error
'program story' 카테고리의 다른 글
Java에서 두 숫자를 곱하면 오버플로가 발생하는지 어떻게 확인할 수 있습니까? (0) | 2020.08.30 |
---|---|
DRY 방식으로 루비의 구조 절에 여러 오류 클래스 전달 (0) | 2020.08.30 |
과거에 두 개의 임의 커밋 사이에 커밋을 삽입하는 방법은 무엇입니까? (0) | 2020.08.30 |
마우스 이벤트없이 jQuery에서 마우스 위치를 얻는 방법은 무엇입니까? (0) | 2020.08.30 |
“.bat”파일에서 명령 줄 매개 변수를 확인하는 방법은 무엇입니까? (0) | 2020.08.30 |