Package javax.persistence

Examples of javax.persistence.EntityManager.createNativeQuery()


    em.persist( item );
    assertTrue( em.contains( item ) );
    em.getTransaction().commit();

    em.getTransaction().begin();
    Query query = em.createNativeQuery( "select * from Item where name = ?1", Item.class );
    query.setParameter( 1, "Mouse" );
    item = (Item) query.getSingleResult();
    assertNotNull( item );
    assertEquals( "Micro$oft mouse", item.getDescr() );
    query = em.createNativeQuery( "select * from Item where name = ?", Item.class );
View Full Code Here


    Query query = em.createNativeQuery( "select * from Item where name = ?1", Item.class );
    query.setParameter( 1, "Mouse" );
    item = (Item) query.getSingleResult();
    assertNotNull( item );
    assertEquals( "Micro$oft mouse", item.getDescr() );
    query = em.createNativeQuery( "select * from Item where name = ?", Item.class );
    query.setParameter( 1, "Mouse" );
    item = (Item) query.getSingleResult();
    assertNotNull( item );
    assertEquals( "Micro$oft mouse", item.getDescr() );
    em.remove( item );
View Full Code Here

    em.flush();
    em.clear();

    assertEquals(
        1, em.createNativeQuery(
        "update Item set descr = 'Logitech Mouse' where name = 'Mouse'"
    ).executeUpdate()
    );
    item = em.find( Item.class, item.getName() );
    assertEquals( "Logitech Mouse", item.getDescr() );
View Full Code Here

    return findAllForType(TagTarget.ARTIST);
  }
 
  private static Collection<Tag> findAllForType(TagTarget tt) {
    EntityManager em=DBController.getEntityManager();
    Query queryTag = em.createNativeQuery(
          "Select * from Tag where target = #tt", Tag.class
      );
    queryTag.setParameter("tt", tt.toString());
    Collection<Tag> tags;
    try {
View Full Code Here

    return findByName(Constants.TYPE_SINGLE_NAME);
  }
 
  public static MediumType findByName(String name) {
    EntityManager em=DBController.getEntityManager();
    Query query = em.createNativeQuery("SELECT * FROM MediumType where name=#tName", MediumType.class);
    query.setParameter("tName", name);
    return (MediumType)query.getSingleResult();
  }
 
  public static void initBasicTypes() {
View Full Code Here

    return castToList(query.getResultList());
  }
 
  public static Collection<Artist> findByName(String n) {
    EntityManager em=DBController.getEntityManager();
    Query queryArtist = em.createNativeQuery(
          "Select * from Artist where name = #aName", Artist.class
      );
    queryArtist.setParameter("aName", n);
    return castToList(queryArtist.getResultList());
  }
View Full Code Here

  private void loadGenres() {
    EntityManagerFactory factory = Persistence
        .createEntityManagerFactory("trackbase");
    EntityManager em = factory.createEntityManager();
    @SuppressWarnings("unchecked")
    List<Genre> g = em
        .createNativeQuery("select * from Genre", Genre.class)
        .getResultList();
    genres = Collections.unmodifiableCollection(g);
  }

View Full Code Here

    return mt;
  }

  public static Collection<Label> findByName(String n) {
    EntityManager em=DBController.getEntityManager();
    Query queryLabel = em.createNativeQuery(
          "Select * from Label where name = #lName", Label.class
      );
    queryLabel.setParameter("lName", n);
    return castToList(queryLabel.getResultList());
  }
View Full Code Here

 
  public void testNativeQueryDeclaredParameters() {
        String sql = "SELECT * FROM Application WHERE id=?1 AND name=?2";
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Query query = em.createNativeQuery(sql);
        assertTrue(query.getParameters().isEmpty());
        em.getTransaction().commit();
        em.close();
  }
 
View Full Code Here

 
  void verifyParams(String jpql, Class<? extends Exception> error,
        Object... params) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Query query = em.createNativeQuery(jpql);
        for (int i = 0; params != null && i < params.length; i = +2) {
            try {
                if (params[i] instanceof Number) {
                    query.setParameter(((Number) params[i]).intValue(),
                        params[i + 1]);
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.