【问题标题】:Spring JDO - makePersistent not returning created objectSpring JDO - makePersistent 不返回创建的对象
【发布时间】:2011-11-13 01:01:51
【问题描述】:

我正在使用 Spring 和 JDO 连接到 MySQL 数据库。 当我持久化一个对象时,我希望看到makePersistent() 方法返回的创建对象。它确实返回了一个对象,但这个对象只有新创建的对象的 ID。所有其他持久化的字段现在都具有 null 值。

在下面的代码示例中,我用carouselNumber 插入了一个值12。返回的对象将该值设置为 NULL,并且 id 具有数据库中新创建的 ID 值。

我使用了JDOHelper.getObjectState(),发现我的对象处于瞬态状态。

我想知道是否缺少一些注释来告诉JDO 返回对象上的所有值,而不仅仅是新生成的 ID。

编辑: 我做了一些进一步的探索,发现当我使用原始 Datanucleus JDP API 时,thsi 工作正常。问题似乎只出现在我使用 Spring 的 JDO 模板时。我真的很想了解为什么会有所不同。谢谢

提前致谢,

布赖恩。

@PersistenceCapable(table = "CAROUSEL", identityType = IdentityType.APPLICATION)

public class Carousel {

    @PrimaryKey(column = "ID")
    @Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
    private Long id;

   @Column(name = "CAROUSEL_NUM")
    private int carourselNumber;

......

public class CarouselDAOImpl extends JdoDaoSupport implements ICarouselDAO {

  public Carousel insert(Carousel carousel) {
    return getJdoTemplate().makePersistent(carousel);
   }

.....

【问题讨论】:

    标签: spring jdo


    【解决方案1】:

    makePersistent 方法会更改您传入的轮播对象以生成 ID,因此它不需要返回任何内容。只需使您的方法无效并使用您传入的轮播。

    【讨论】:

    • 嗨,戴夫。是的,我意识到这一点。我已经尝试了你的建议,问题是一样的。我可以看到使用 Springs JDOTemplate 的不同之处在于它使对象瞬态,而直接使用 JDO API 对象返回为空心的。这意味着当我调用 getCarouselNumber 时,它会延迟加载值。
    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 2013-02-21
    • 2013-04-14
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2014-07-07
    • 2021-11-14
    相关资源
    最近更新 更多