From bf6915d81a3cf770eedd8f38e90073ab7a87c649 Mon Sep 17 00:00:00 2001 From: erik Date: Mon, 18 Feb 2008 22:16:57 +0000 Subject: [PATCH] Now using JPA with a container managed entity manager. Also corrected the mapping for composite key with many to one. --- mythtv/monitor/pom.xml | 8 ++ .../main/java/org/wamblee/mythtv/Channel.java | 20 +++- .../java/org/wamblee/mythtv/Recording.java | 94 ++++++++++++++----- .../org/wamblee/mythtv/RecordingDatabase.java | 25 +++-- .../java/org/wamblee/mythtv/RecordingPk.java | 67 ++++++++++++- .../main/resources/META-INF/persistence.xml | 17 ++++ .../war/src/main/resources/beanRefContext.xml | 7 +- .../org.wamblee.mythtv.application.xml | 11 ++- .../org.wamblee.mythtv.datasource.xml | 7 +- .../org.wamblee.mythtv.hibernate.xml | 27 +++--- .../org.wamblee.mythtv.properties.xml | 7 +- mythtv/war/src/webapp/WEB-INF/web.xml | 4 + pom.xml | 15 ++- 13 files changed, 241 insertions(+), 68 deletions(-) create mode 100644 mythtv/monitor/src/main/resources/META-INF/persistence.xml diff --git a/mythtv/monitor/pom.xml b/mythtv/monitor/pom.xml index 9c031a2f..03fa2edd 100644 --- a/mythtv/monitor/pom.xml +++ b/mythtv/monitor/pom.xml @@ -28,6 +28,14 @@ javax.persistence persistence-api + + org.springframework + spring-jpa + + + concurrent + concurrent + log4j log4j diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/Channel.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/Channel.java index e5ef7047..d519c9ad 100644 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/Channel.java +++ b/mythtv/monitor/src/main/java/org/wamblee/mythtv/Channel.java @@ -28,11 +28,8 @@ import javax.persistence.Table; @Table(name="channel") public class Channel { - @Id - @Column(name="chanid") private int _id; - @Column(name="name") private String _name; protected Channel() { @@ -42,17 +39,34 @@ public class Channel { /** * @return the id */ + @Id + @Column(name="chanid") public int getId() { return _id; } + + /** + * @param aId the id to set + */ + public void setId(int aId) { + _id = aId; + } /** * @return the name */ + @Column(name="name") public String getName() { return _name; } + /** + * @param aName the name to set + */ + public void setName(String aName) { + _name = aName; + } + /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/Recording.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/Recording.java index ca0ab1cb..da7220de 100644 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/Recording.java +++ b/mythtv/monitor/src/main/java/org/wamblee/mythtv/Recording.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.util.Date; import javax.persistence.Column; +import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; @@ -27,36 +28,27 @@ import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.persistence.Transient; /** * */ @Entity @Table(name="recorded") -//@IdClass(RecordingPk.class) public class Recording implements Serializable { + + private RecordingPk _id; - //@Id - @ManyToOne(targetEntity=Channel.class) - @JoinColumn(name="chanid") - private Channel _channel; - @Id - @Column(name="starttime") - private Date _starttime; - - @Column(name="basename") private String _basename; - @Column(name="progstart") private Date _progstart; - @Column(name="title") private String _title; - - @Column(name="subtitle") + private String _subtitle; - @Column(name="filesize") private long _filesize; protected Recording() { @@ -66,48 +58,100 @@ public class Recording implements Serializable { /** * @return the channel */ - public Channel getChannel() { - return _channel; + //@Id + //@ManyToOne(targetEntity=Channel.class) + //@JoinColumn(name="chanid") + + /** + * @return the id + */ + @EmbeddedId + public RecordingPk getId() { + return _id; } - + + /** + * @param aId the id to set + */ + public void setId(RecordingPk aId) { + _id = aId; + } + /** * @return the basename */ + @Column(name="basename") public String getBasename() { return _basename; } - + /** - * @return the progstart + * @param aBasename the basename to set */ - public Date getStartTime() { - return _starttime; + public void setBasename(String aBasename) { + _basename = aBasename; } /** * @return the progstart */ + @Column(name="progstart") + @Temporal(TemporalType.TIMESTAMP) public Date getProgstart() { return _progstart; } + /** + * @param aProgstart the progstart to set + */ + public void setProgstart(Date aProgstart) { + _progstart = aProgstart; + } + + @Transient + public Channel getChannel() { + return _id.getChannel(); + } + + @Transient + public Date getStarttime() { + return _id.getStartTime(); + } + /** * @return the title */ + @Column(name="title") public String getTitle() { return _title; } + + /** + * @param aTitle the title to set + */ + public void setTitle(String aTitle) { + _title = aTitle; + } /** * @return the subtitle */ + @Column(name="subtitle") public String getSubtitle() { return _subtitle; } + /** + * @param aSubtitle the subtitle to set + */ + public void setSubtitle(String aSubtitle) { + _subtitle = aSubtitle; + } + /** * @return the filesize */ + @Column(name="filesize") public long getFilesize() { return _filesize; } @@ -121,7 +165,7 @@ public class Recording implements Serializable { */ @Override public String toString() { - return "Recording(" + _channel + "," + _basename + "," + _progstart + "," + _title + "," + _subtitle + ")"; + return "Recording(" + _id + "," + _basename + "," + _progstart + "," + _title + "," + _subtitle + ")"; } /* (non-Javadoc) @@ -133,7 +177,7 @@ public class Recording implements Serializable { return false; } Recording recording = (Recording)aObj; - return _channel.equals(recording._channel) && _starttime.equals(recording._starttime); + return _id.equals(recording._id); } /* (non-Javadoc) @@ -141,7 +185,7 @@ public class Recording implements Serializable { */ @Override public int hashCode() { - return _channel.hashCode()*10 + ((int)_starttime.getTime() %10); + return _id.hashCode(); } } diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingDatabase.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingDatabase.java index 7ff51709..e4badfba 100644 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingDatabase.java +++ b/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingDatabase.java @@ -21,6 +21,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.Query; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; @@ -28,11 +31,13 @@ import org.hibernate.Session; import org.hibernate.criterion.Expression; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; +import org.springframework.orm.jpa.JpaTemplate; +import org.springframework.orm.jpa.support.JpaDaoSupport; /** * */ -public class RecordingDatabase extends HibernateDaoSupport { +public class RecordingDatabase extends JpaDaoSupport { private static final Log LOG = LogFactory.getLog(RecordingDatabase.class); @@ -50,14 +55,12 @@ public class RecordingDatabase extends HibernateDaoSupport { } public Recording findRecording(final String aName) { - List result = (List) getHibernateTemplate().execute(new HibernateCallback() { - /* (non-Javadoc) - * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session) - */ - public Object doInHibernate(Session aSession) throws HibernateException, SQLException { - return aSession.createCriteria(Recording.class).add(Expression.eq("_basename", aName)).list(); - } - }); + JpaTemplate jpaTemplate = getJpaTemplate(); + EntityManager entityManager = jpaTemplate.getEntityManager(); + Query query = entityManager.createQuery( + "from org.wamblee.mythtv.Recording where basename = ?1"); + query.setParameter(1, aName); + List result = query.getResultList(); if ( result.size() > 1 ) { throw new RuntimeException("More than two recordings returned"); } @@ -68,6 +71,8 @@ public class RecordingDatabase extends HibernateDaoSupport { } public void update(Recording aRecording) { - getHibernateTemplate().update(aRecording); + // Update is not required since the whole task of updating the + // directory structure occurs within a single transaction. + // Therefore, modifications to recordings are automatically persisted. } } diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingPk.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingPk.java index d47af6d7..da06d3d0 100644 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingPk.java +++ b/mythtv/monitor/src/main/java/org/wamblee/mythtv/RecordingPk.java @@ -16,16 +16,81 @@ package org.wamblee.mythtv; +import java.io.Serializable; import java.util.Date; import javax.persistence.Column; +import javax.persistence.Embeddable; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; /** * */ -public class RecordingPk { +@Embeddable +public class RecordingPk implements Serializable { public Channel _channel; public Date _starttime; + + public RecordingPk() { + // Empty + } + + /** + * @return the channel + */ + @ManyToOne + @JoinColumn(name="chanid") + public Channel getChannel() { + return _channel; + } + + /** + * @param aChannel the channel to set + */ + public void setChannel(Channel aChannel) { + _channel = aChannel; + } + + /** + * @return the starttime + */ + @Column(name="starttime") + @Temporal(TemporalType.TIMESTAMP) + public Date getStartTime() { + return _starttime; + } + + /** + * @param aStarttime the starttime to set + */ + public void setStartTime(Date aStarttime) { + _starttime = aStarttime; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object aObj) { + if ( aObj == null ) { + return false; + } + if ( !(aObj instanceof RecordingPk)) { + return false; + } + RecordingPk pk = (RecordingPk) aObj; + return _channel.equals(pk._channel) && _starttime.equals(pk._starttime); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return _channel.hashCode() + _starttime.hashCode(); + } } diff --git a/mythtv/monitor/src/main/resources/META-INF/persistence.xml b/mythtv/monitor/src/main/resources/META-INF/persistence.xml new file mode 100644 index 00000000..fc87ef01 --- /dev/null +++ b/mythtv/monitor/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,17 @@ + + + org.hibernate.ejb.HibernatePersistence + jdbc/mythtv + org.wamblee.mythtv.Channel + org.wamblee.mythtv.Recording + true + + + \ No newline at end of file diff --git a/mythtv/war/src/main/resources/beanRefContext.xml b/mythtv/war/src/main/resources/beanRefContext.xml index 5288aba3..1bd48a62 100644 --- a/mythtv/war/src/main/resources/beanRefContext.xml +++ b/mythtv/war/src/main/resources/beanRefContext.xml @@ -1,7 +1,8 @@ - - - + diff --git a/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml b/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml index 1a8d0106..dafa987d 100644 --- a/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml +++ b/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml @@ -1,7 +1,8 @@ - - - + @@ -24,8 +25,8 @@ - - + + diff --git a/mythtv/war/src/main/resources/org.wamblee.mythtv.datasource.xml b/mythtv/war/src/main/resources/org.wamblee.mythtv.datasource.xml index 9caa1c88..4f40b6d8 100644 --- a/mythtv/war/src/main/resources/org.wamblee.mythtv.datasource.xml +++ b/mythtv/war/src/main/resources/org.wamblee.mythtv.datasource.xml @@ -1,7 +1,8 @@ - - - + diff --git a/mythtv/war/src/main/resources/org.wamblee.mythtv.hibernate.xml b/mythtv/war/src/main/resources/org.wamblee.mythtv.hibernate.xml index 03a664af..fb66e86b 100644 --- a/mythtv/war/src/main/resources/org.wamblee.mythtv.hibernate.xml +++ b/mythtv/war/src/main/resources/org.wamblee.mythtv.hibernate.xml @@ -1,11 +1,11 @@ - - + - + - + @@ -25,24 +24,26 @@ org.wamblee.mythtv.Recording - - + + - + + + + java:comp/env/persistence/mythtv + + diff --git a/mythtv/war/src/main/resources/org.wamblee.mythtv.properties.xml b/mythtv/war/src/main/resources/org.wamblee.mythtv.properties.xml index 6ba78cef..32fa83cd 100644 --- a/mythtv/war/src/main/resources/org.wamblee.mythtv.properties.xml +++ b/mythtv/war/src/main/resources/org.wamblee.mythtv.properties.xml @@ -1,7 +1,8 @@ - - - + diff --git a/mythtv/war/src/webapp/WEB-INF/web.xml b/mythtv/war/src/webapp/WEB-INF/web.xml index eb64edc6..8d08eb7c 100644 --- a/mythtv/war/src/webapp/WEB-INF/web.xml +++ b/mythtv/war/src/webapp/WEB-INF/web.xml @@ -9,6 +9,10 @@ org.wamblee.mythtv.Application + + persistence/mythtv + +