如何在定义关系的同时直接访问映射字段

How can I access the mapping field directly whilst also having a relationship defined

@Entity
@Table(name="sometable_citylocation")
public class CityLocation extends Model implements Serializable {


    private int cityDestinationId;

    @ManyToOne
    @JoinColumn(name="cityDestinationId", referencedColumnName="destinationId")
    private City city;

我有这种关系,我可以通过定义的映射轻松获得城市,但我还需要能够直接设置和更改 cityDestinationId,因为它是由外部源提供给我的。

我需要什么注释才能在不丢失任何功能的情况下做到这一点(将城市作为对象,能够从 field/getters/setters 获取 set/alter/get id)

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'modelDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory nl.exit.crunch.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [nl/exit/crunch/config/HibernateConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: nl.exit.crunch.table.some.destination.CityLocation column: cityDestinationId (should be mapped with insert="false" update="false")

答案是给对象映射加上insertable和updateable

@JoinColumn(name="cityDestinationId",
            insertable=false, 
            updatable=false, 
            referencedColumnName="destinationId"
        )

请记住,您不能将对象用于 bind/unbind 关系。这与我的情况无关,因为外键将从外部来源提供。我只关心把东西拿出来