我正在尝试使用JPA和JSF-primefaces的Web项目。 我有插入/更新具有@ManyToOne关系的对象的错误。
插入后,@ JoinColumn为空白字段。 (抱歉,不发布图片,这是图片链接)
然后,在重新加载列表后,空白字段恢复正常
我正在使用单向@ManyToOne实体
Place.java
@Entity
@Table(name = "place")
@XmlRootElement
public class Place implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
...
@Basic(optional = false)
@NotNull
@Column(name = "prefecture_id")
private int prefectureId;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "prefecture_id",referencedColumnName = "id", updatable = false,insertable = false)
private Aken prefecture;
Aken.java
@Entity
@Table(name = "aken")
@XmlRootElement
public class Aken implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Short id;
@Size(max = 50)
@Column(name = "data")
private String data;
方法更新到数据库
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class PlaceFacade extends AbstractFacade<Place> {
@PersistenceContext(unitName = "JutenPU")
private EntityManager em;
@Resource
private UserTransaction userTransaction;
public void edit(Place place){
try{
getUserTransaction().begin();
getEntityManager().merge(place);
getUserTransaction().commit();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
方法检索表
上的数据public List<Place> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(Place.class));
return getEntityManager().createQuery(cq).getResultList();
}
我正在使用JAVA EE7,glassfish服务器4,primefaces 4.0
感谢您的帮助
答案 0 :(得分:0)
@JoinColumn(name = "prefecture_id",referencedColumnName = "id", updatable = false,insertable = false)
您将外键列标记为READONLY。
删除限制:@JoinColumn(name = "prefecture_id",referencedColumnName = "id")