Этого можно добиться с помощью @IdClass и @ GeneratedValue.
Пример кода:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="LEDGER")
@IdClass( PK.class )
public class Ledger {
@Id
@Column(name = "log_id")
@GeneratedValue
public int logId;
@Id
@Column(name = "order_id")
public int orderId;
public String otherFields;
}
Класс Id
import java.io.Serializable;
public class PK implements Serializable{
public int logId;
public int orderId;
public PK() {
}
public PK(int logId, int orderId) {
super();
this.logId = logId;
this.orderId = orderId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + logId;
result = prime * result + orderId;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PK other = (PK) obj;
if (logId != other.logId)
return false;
if (orderId != other.orderId)
return false;
return true;
}
}
Для дальнейшего чтения перейдите по ссылкам ниже
- IdClass
- GeneratedValue
- Примеры