jpa技术总结 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-13
ot; nullable="false" precision="18" scale="0"/>
attribute-override>
<attributes>
<basic name="text">
<column name="MESSAGE_TEXT" nullable="false" />
basic>
attributes>
entity>
entity-mappings>
实体定义,用了继承,不用继承的早就解决了。注意@MappedSuperclass ,@entity,@Id的用法。需要jpa实现的包。我用的hibernate,这些包主要来自hibernate core 3.2.0g, hibernate entitymanager 3.2.0 and hibernate annotation 3.2. 具体要用其中的哪些我还没怎么弄明白,一个个加吧。 java 代码 package hello; import java.io.Serializable; import javax.persistence.Id; import javax.persistence.MappedSuperclass; @MappedSuperclass public class BaseMessage implements Serializable { private static final long serialVersionUID = 4585624359812256628L; private Long id; public BaseMessage() { super(); } @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } } package hello; import javax.persistence.Entity; @Entity public class Message extends BaseMessage { private static final long serialVersionUID = -7660981805652763488L; private String text; Message() { } public Message(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } } jpa技术总结(3)时间:2012-01-03persistence 过程: java 代码 package hello; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class HelloWorldEM { public static void main(String[] args) { // Start EntityManagerFactory EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld"); // First unit of work EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Message message = new Message("Hello World"); em.persist(message); Long msgId = message.getId(); tx.commit(); em.close(); // Second unit of work EntityManager newEm = emf.createEntityManager(); EntityTransaction newTx = newEm.getTransaction(); newTx.begin(); List messages = newEm.createQuery("select m from Message m order by m.text asc").getResultList(); System.out.println(messages.size() + " message(s) found"); for (Object m : messages) { Message loadedMsg = (Message) m; System.out.println(msgId.longValue()); System.out.println(loadedMsg.getText()); } newTx.commit(); newEm.close(); emf.close(); } } |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于jpa技术总结 - 编程入门网的所有评论